From b0c48e7981f3926fe3e63c31ad1880f083556888 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Fri, 6 Nov 2026 10:45:06 -0500 Subject: [PATCH 01/13] policy: cross-check the loaded CMDB against host_specific.json, refuse to actuate on any difference (Q10, proposal 13 option 3) --- .../tendcf-validate-proposal-2026-11-03.md | 37 +++++ policy/tendcf_services.cf | 152 ++++++++++++++++- .../policy/fixtures/interlock-malformed.json | 1 + tests/policy/fixtures/runit-pair.json | 1 + tests/policy/run.sh | 155 +++++++++++++++++- tests/policy/sim-loader.cf | 46 ++++++ 6 files changed, 388 insertions(+), 4 deletions(-) create mode 100644 tests/policy/fixtures/interlock-malformed.json create mode 100644 tests/policy/fixtures/runit-pair.json create mode 100644 tests/policy/sim-loader.cf diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index afb67d8..1e6d78e 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1127,6 +1127,43 @@ also bears on pastebin, which already is (see above). Run test 1 against the current policy first: it must actuate the service. That reproduces the fail-open before the fix. +**Implemented 2026-11-06: option 3, per djbclark's ruling Q10 (2026-11-02).** +`bundle common tendcf_cmdb` in `policy/tendcf_services.cf` reads +`$(sys.workdir)/data/host_specific.json` with `readjson` (5 MiB cap) and +compares it with the data the loader installed. For every `vars` key it +requires `data:variables.` to exist and the key's entry ids to equal +the loaded container's indices (a half-loaded container is caught). It also +refuses a loaded `data:variables.*` that is not a `vars` key of the file, a +file that exists but cannot be parsed, and loaded data with no file. A missing +file with nothing loaded is the normal empty case and says nothing. On any +mismatch it defines the one class `tendcf_cmdb_refused`, and +`tendcf_interlocks` reports what differs once. Both bundles then see empty +`interlock_ids` and `service_ids`: no pre_action, no service, no tombstone. +It is a `bundle common`, so cf-agent evaluates it to completion before the +first agent bundle whatever `bundlesequence` says, which is what makes the +class final before any gate is read. `bundlesequence` guard classes in +`promises.cf` are not needed for this check. The fork-side all-or-nothing +loader (PR #41) and this check are independent: with the loader failing +closed, the file's keys are not loaded and the check refuses and reports +it, which is correct and louder. + +Two findings from the implementation, both about the proof above. (1) The +Mac dev build (`3.29.0a.086d27749`) does not carry the per-entry loader +(`8f0076b81` is not an ancestor): an unresolved `$(...)` anywhere in `vars` +makes it install nothing at all, so the fail-open cannot be reproduced there +with a file. `tests/policy/sim-loader.cf` therefore installs +`data:variables.*` in the loader's place, from the same file, to emulate the +per-entry skip; the original policy was run against it first and did +process the service with its interlock skipped. The Linux CI legs build the +pinned fork and so also exercise the real per-entry skip with the literal +fixture (`tests/policy/fixtures/interlock-malformed.json`). (2) The check +needs `readjson` and `variablesmatching` to compose on the loader's +installed variables; they do on the dev build (checks 9-10 of +`tests/policy/run.sh`), so option 4 stays unneeded. The policy-tree digest +changes with this change (§13(b) option 3, §13(c)). + +PR: https://github.com/frdminc/tendcf/pull/42 + ## 14. Review record Gemini 4 Argon reviewed commit `a7b7c31` (2026-11-02; verdict "sound diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index a993615..95a3da1 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -59,12 +59,135 @@ # commands: daemon-reload, enable/disable, start, restart, teardown, # reload after removal. State probes are snapshots (see the classes # section), so one run converges and the next does nothing. +# +# CMDB cross-check (`tendcf_cmdb`, proposal 2026-11-03 section 13, option 3, +# operator ruling Q10 2026-11-02). Everything above assumes CFEngine's loader +# installed what host_specific.json says. It does not promise that: the fork's +# loader skips a bad entry on its own and still returns success, so a skipped +# `tendcf_interlock` leaves every service ungated (services are gated +# negatively, on `!interlock_blocked_*`, and that class only exists for an +# interlock that loaded). The check does not model WHY an entry was dropped. It +# reads the file itself and compares it with what the loader installed: +# - for every top-level key under `vars`, `data:variables.` must exist; +# - per key, the set of entry ids in the file must equal the set of +# indices of the loaded container (a half-loaded container is caught); +# - every `data:variables.*` the loader installed must be a key of `vars` +# in the file (it also catches a `variables` section in the file); +# - a file that exists but cannot be parsed (or exceeds the loader's own +# 5 MiB cap), and loaded data with no file at all, are mismatches. +# A missing file with nothing loaded is the normal empty case: no class, no +# report. A key the loader files elsewhere (`a.b`, `ns:b.c`) cannot match +# `data:variables.`, so it reads as a mismatch: refused, never ignored. +# +# On any mismatch the single class `tendcf_cmdb_refused` is defined and +# NOTHING is actuated: no interlock pre_action, no service, no tombstone. The +# gate is one place per bundle: `interlock_ids` and `service_ids` are empty +# under the refusal, and every promise that acts iterates one of them +# (the two daemon-reload commands are guarded by classes only those promises +# set). A new actuating promise MUST iterate those ids or carry +# `!tendcf_cmdb_refused` itself. The mismatch is reported once, by +# tendcf_interlocks. +# +# Order is load-bearing, as in the paragraph above: the check is a +# `bundle common`, which cf-agent evaluates to completion before any agent +# bundle, whatever `bundlesequence` says, so `tendcf_cmdb_refused` is final +# before the first gated class is read. It would not be inside an agent +# bundle that also gates (a class read before its definition is false and, +# once defined, never undone), and the check must not read a class it defines +# in a negative form for the same reason: only positive evidence defines a +# `tendcf_cmdb_problem_*` class. Proved on a real cf-agent by +# tests/policy/run.sh checks 9-10 (a pre_action must not run, a service must +# not be processed). +# +# What it cannot see: a `classes` section in the file (loaded as classes, not +# variables). The projector never emits one (N-2). + +bundle common tendcf_cmdb +{ + vars: + "file" + string => "$(sys.workdir)/data/host_specific.json"; + + # 5242880 = the loader's HOST_SPECIFIC_DATA_MAX_SIZE (5 MiB). An absent + # file leaves `doc` undefined and is not an error. + "doc" + data => readjson("$(file)", 5242880), + ifvarclass => fileexists("$(file)"); + + "file_keys" + slist => getindices("doc[vars]"), + ifvarclass => isvariable("doc[vars]"); + + "file_keys" + slist => { }, + ifvarclass => not(isvariable("doc[vars]")); + + "loaded_refs" + slist => variablesmatching("^data:variables\..*"); + + "loaded_keys" + slist => maplist(regex_replace("$(this)", "^data:variables\.", "", ""), + "loaded_refs"); + + "missing_keys" + slist => difference("file_keys", "loaded_keys"); + + "unlisted_keys" + slist => difference("loaded_keys", "file_keys"); + + "key_canon[$(file_keys)]" + string => canonify("$(file_keys)"); + + "file_ids[$(file_keys)]" + slist => getindices("doc[vars][$(file_keys)]"); + + "loaded_ids[$(file_keys)]" + slist => getindices("data:variables.$(file_keys)"); + + "unloaded_ids[$(file_keys)]" + slist => difference("file_ids[$(file_keys)]", "loaded_ids[$(file_keys)]"); + + "unfiled_ids[$(file_keys)]" + slist => difference("loaded_ids[$(file_keys)]", "file_ids[$(file_keys)]"); + + "unloaded_text[$(file_keys)]" + string => join(", ", "unloaded_ids[$(file_keys)]"); + + "unfiled_text[$(file_keys)]" + string => join(", ", "unfiled_ids[$(file_keys)]"); + + classes: + "tendcf_cmdb_problem_unreadable" + expression => and(fileexists("$(file)"), + not(isvariable("tendcf_cmdb.doc"))); + + "tendcf_cmdb_problem_nofile" + expression => and(not(fileexists("$(file)")), + isgreaterthan(length("loaded_keys"), 0)); + + "tendcf_cmdb_problem_missing" + expression => isgreaterthan(length("missing_keys"), 0); + + "tendcf_cmdb_problem_unlisted" + expression => isgreaterthan(length("unlisted_keys"), 0); + + "tendcf_cmdb_problem_partial_$(key_canon[$(file_keys)])" + expression => and(isvariable("data:variables.$(file_keys)"), + or(isgreaterthan(length("unloaded_ids[$(file_keys)]"), 0), + isgreaterthan(length("unfiled_ids[$(file_keys)]"), 0))); + + "tendcf_cmdb_refused" + expression => classmatch("tendcf_cmdb_problem_.*"); +} bundle agent tendcf_interlocks { vars: + # Empty under a CMDB mismatch (see `tendcf_cmdb`): with no ids no + # pre_action command is even built. "interlock_ids" - slist => getindices("data:variables.tendcf_interlock"); + slist => getindices("data:variables.tendcf_interlock"), + ifvarclass => "!tendcf_cmdb_refused"; "interlock_canon[$(interlock_ids)]" string => canonify("$(interlock_ids)"); @@ -139,16 +262,39 @@ bundle agent tendcf_interlocks # still is. "tendcf: interlock '$(interlock_ids)' pre_action command[0] is not an absolute path; refused" if => not(regcmp("/.*", "$(pre_action_cmd[$(interlock_ids)])")); + + # The one report of a CMDB mismatch (tendcf_cmdb decides, this says). + # First bundle of the sequence, so it comes before anything else. + "tendcf_cmdb_refused":: + "tendcf: CMDB cross-check failed: refusing to actuate anything (no interlock pre_action, no service, no tombstone) until $(tendcf_cmdb.file) and the data CFEngine loaded from it agree"; + + "tendcf_cmdb_problem_unreadable":: + "tendcf: CMDB cross-check: $(tendcf_cmdb.file) exists but cannot be read as JSON (parse error or larger than 5242880 bytes)"; + + "tendcf_cmdb_problem_nofile":: + "tendcf: CMDB cross-check: $(tendcf_cmdb.file) does not exist but CFEngine loaded data:variables.$(tendcf_cmdb.loaded_keys)"; + + "tendcf_cmdb_problem_missing":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.missing_keys)' is under 'vars' in $(tendcf_cmdb.file) but CFEngine did not load data:variables.$(tendcf_cmdb.missing_keys) (the loader skipped it)"; + + "tendcf_cmdb_problem_unlisted":: + "tendcf: CMDB cross-check: CFEngine loaded data:variables.$(tendcf_cmdb.unlisted_keys), which is not a key under 'vars' in $(tendcf_cmdb.file)"; + + "tendcf_cmdb_problem_partial_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.file_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.file_keys)' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.file_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.file_keys)])]"; } bundle agent tendcf_services { vars: + # Empty under a CMDB mismatch, see tendcf_interlocks and `tendcf_cmdb`. "service_ids" - slist => getindices("data:variables.tendcf_service"); + slist => getindices("data:variables.tendcf_service"), + ifvarclass => "!tendcf_cmdb_refused"; "interlock_ids" - slist => getindices("data:variables.tendcf_interlock"); + slist => getindices("data:variables.tendcf_interlock"), + ifvarclass => "!tendcf_cmdb_refused"; "service_canon[$(service_ids)]" string => canonify("$(service_ids)"); diff --git a/tests/policy/fixtures/interlock-malformed.json b/tests/policy/fixtures/interlock-malformed.json new file mode 100644 index 0000000..181b1bf --- /dev/null +++ b/tests/policy/fixtures/interlock-malformed.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false","$(sys.workdir)"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}}} diff --git a/tests/policy/fixtures/runit-pair.json b/tests/policy/fixtures/runit-pair.json new file mode 100644 index 0000000..40447e7 --- /dev/null +++ b/tests/policy/fixtures/runit-pair.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"},"demo-runit-two":{"bundle":"demo","command":["/bin/sleep","2"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}}} diff --git a/tests/policy/run.sh b/tests/policy/run.sh index c801c5c..4b89583 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -51,6 +51,25 @@ # schema alphabet admits plus a sample of letters and digits, and a # metacharacter set); 8d refuses a relative command[0] from a projection # that skipped the schema. +# 9. CMDB cross-check against the agent's real loader (tendcf_cmdb in +# policy/tendcf_services.cf, proposal 2026-11-03 section 13 option 3): a +# file the loader cannot install whole is refused and reported and +# actuates nothing; the consistent files of checks 2-4 raise no report; no +# file at all, and a file with no vars, are the normal empty case. NB: the +# fork's loader skips a bad entry and installs the rest, but the Mac dev +# build rejects the whole `vars` instead (the same fixture refuses on both, +# as "nothing loaded"), so only the fork's CI legs see the per-entry skip. +# 10. the same cross-check against a loader that skips ONE container, which +# the Mac dev build cannot do: tests/policy/sim-loader.cf installs +# data:variables.* in the loader's place (cf-agent --no-host-specific-data) +# from the very file the policy reads. Each case must report what differs +# and run nothing; the control case (everything installed) must run as +# before, so the others cannot pass because the stand-in installed nothing. +# A skipped interlock container with the services loaded is the 2026-11-02 +# fail-open; a skipped service container with the interlock loaded shows +# the first bundle (tendcf_interlocks, which runs pre_action commands) is +# gated too. Under TENDCF_TEST_SYSTEMD=1 the same stand-in is run against +# the real manager (6b): no unit rendered, a tombstone leaves the unit alone. # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist # on Linux and macOS alike; /bin/true does not exist on macOS, and a missing @@ -102,6 +121,14 @@ body common control bundlesequence => { "tendcf_interlocks", "tendcf_services" }; } EOF +# Same two bundles, with the stand-in loader first (see tests/policy/sim-loader.cf). +cat > "$work/inputs/sim-entry.cf" < { "$repo/tests/policy/sim-loader.cf", "$repo/policy/tendcf_services.cf" }; + bundlesequence => { "tendcf_interlocks", "tendcf_services" }; +} +EOF fail=0 check() { # check NAME COND... @@ -109,6 +136,17 @@ check() { # check NAME COND... if "$@"; then echo "ok $name"; else echo "FAIL $name"; fail=1; fi } +# The cross-check's report. "refusing to actuate anything" is the summary line +# tendcf_interlocks prints once; no_actuation is what a refused run must not do: +# run any command (-I logs every one, pre_action included), render, or process +# a service or an interlock entry at all (each prints a "tendcf: service|interlock" +# report when it is processed, and `*_ids` are empty under a refusal). +refusal_reported() { grep -q "tendcf: CMDB cross-check failed: refusing to actuate anything" "$1"; } +refusal_once() { [ "$(grep -c "CMDB cross-check failed" "$1")" = 1 ]; } +no_crosscheck() { ! grep -q "CMDB cross-check" "$1"; } +no_actuation() { ! grep -Eq "Executing|Created file|launchctl|systemctl|tendcf: (service|interlock) '" "$1"; } +key_not_loaded() { grep -q "key '$2' is under 'vars' in .* but CFEngine did not load data:variables.$2" "$1"; } + echo "# 1. wiring: cf-promises with examples/host_specific.json" cp "$repo/examples/host_specific.json" "$work/data/host_specific.json" check "cf-promises parses the bundle with the example projection" \ @@ -215,6 +253,22 @@ if [ "$(uname)" = Linux ] && [ -d /run/systemd/system ]; then unit_enabled() { as_root systemctl is-enabled --quiet "$unit"; } main_pid() { systemctl show -p MainPID --value "$unit"; } + sysd_run_sim() { # sysd_run_sim FIXTURE CLASSES OUT: stand-in loader, run as root + cp "$repo/tests/policy/fixtures/$1" "$work/data/host_specific.json" + as_root env CFENGINE_TEST_OVERRIDE_WORKDIR="$work" \ + "$cf_agent" -K -v --no-host-specific-data -D "$2" -f "$work/inputs/sim-entry.cf" >"$3" 2>&1 || true + as_root chown -R "$(id -u):$(id -g)" "$work" + } + + # 6b. Refused by the CMDB cross-check (check 10): the interlock container + # was skipped, the service loaded. Nothing may be rendered or started. + sysd_run_sim systemd-present.json sim_service "$work/sysd-sim-present.out" + check "cross-check refusal on a real systemd host: reported" \ + refusal_reported "$work/sysd-sim-present.out" + check "cross-check refusal: no unit rendered" bash -c "[ ! -e '$unit_path' ]" + check "cross-check refusal: no systemctl command or probe" \ + no_systemctl "$work/sysd-sim-present.out" + sysd_run systemd-present.json "$work/sysd1.out" check "no refusal reported on a normal systemd host" \ bash -c "! grep -q \"'$unit' refused\" '$work/sysd1.out'" @@ -230,6 +284,16 @@ if [ "$(uname)" = Linux ] && [ -d /run/systemd/system ]; then check "unit enabled after the first run" unit_enabled pid1="$(main_pid)" + # 6b, tombstone: the unit is live; a refused run must leave it alone. + sysd_run_sim systemd-absent.json sim_service "$work/sysd-sim-tomb.out" + check "cross-check refusal (tombstone): reported" \ + refusal_reported "$work/sysd-sim-tomb.out" + check "cross-check refusal (tombstone): unit file left in place" \ + bash -c "[ -f '$unit_path' ]" + check "cross-check refusal (tombstone): unit still active" unit_active + check "cross-check refusal (tombstone): no systemctl command or probe" \ + no_systemctl "$work/sysd-sim-tomb.out" + sysd_run systemd-present.json "$work/sysd2.out" check "second run issues no systemctl command" no_systemctl "$work/sysd2.out" check "second run leaves the same process" \ @@ -450,8 +514,97 @@ check "relative command[0]: the reason is reported" \ check "relative command[0]: nothing was run" \ bash -c "[ ! -e '$work/rel/ran' ]" +echo "# 9. CMDB cross-check, the agent's real loader" +cp "$repo/tests/policy/fixtures/interlock-malformed.json" "$work/data/host_specific.json" +out="$work/xc-malformed.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "malformed interlock: refusal reported" refusal_reported "$out" +check "malformed interlock: the container the loader did not install is named" \ + key_not_loaded "$out" tendcf_interlock +check "malformed interlock: refusal reported once" refusal_once "$out" +check "malformed interlock: nothing actuated, no pre_action run, no service processed" \ + no_actuation "$out" +for f in refused satisfied flavour; do + check "consistent file of the $f run (checks 2-4): no cross-check report" no_crosscheck "$work/$f.out" +done +rm -f "$work/data/host_specific.json" +out="$work/xc-nofile.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "no host_specific.json at all: no report" no_crosscheck "$out" +check "no host_specific.json at all: no error logged" bash -c "! grep -q 'error:' '$out'" +check "no host_specific.json at all: nothing actuated" no_actuation "$out" +printf '%s\n' '{"vars":{}}' >"$work/data/host_specific.json" +out="$work/xc-novars.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "empty vars: no report" no_crosscheck "$out" +check "empty vars: nothing actuated" no_actuation "$out" +printf '%s\n' '{bad' >"$work/data/host_specific.json" +out="$work/xc-garbage.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "unparseable file: refusal reported" refusal_reported "$out" +check "unparseable file: reported as unreadable" \ + grep -q "host_specific.json exists but cannot be read as JSON" "$out" +check "unparseable file: refusal reported once" refusal_once "$out" +check "unparseable file: nothing actuated" no_actuation "$out" + +echo "# 10. CMDB cross-check, a loader that skips one container (tests/policy/sim-loader.cf)" +sim_run() { # sim_run FIXTURE|none CLASSES OUT + if [ "$1" = none ]; then rm -f "$work/data/host_specific.json" + else cp "$repo/tests/policy/fixtures/$1" "$work/data/host_specific.json"; fi + "$cf_agent" -K -I --no-host-specific-data -D "$2" -f "$work/inputs/sim-entry.cf" >"$3" 2>&1 || true +} +out="$work/xc-sim-control.out" +sim_run flavour-refused.json sim_interlock,sim_service "$out" +check "control (everything installed): no cross-check report" no_crosscheck "$out" +check "control: the pre_action ran" grep -q "Executing .*'/usr/bin/true'" "$out" +check "control: the service was processed" \ + grep -q "tendcf: service 'demo-runit' refused: unit flavour 'runit'" "$out" + +# The 2026-11-02 fail-open: the services loaded, their interlock did not. +out="$work/xc-sim-nointerlock.out" +sim_run flavour-refused.json sim_service "$out" +check "interlock container skipped: refusal reported" refusal_reported "$out" +check "interlock container skipped: tendcf_interlock named" key_not_loaded "$out" tendcf_interlock +check "interlock container skipped: tendcf_service not blamed" \ + bash -c "! grep -q \"key 'tendcf_service'\" '$out'" +check "interlock container skipped: no service processed, nothing actuated" no_actuation "$out" + +# The reverse, which also proves the FIRST bundle is gated: the interlock +# loaded, so tendcf_interlocks would run its pre_action. +out="$work/xc-sim-noservice.out" +sim_run flavour-refused.json sim_interlock "$out" +check "service container skipped: refusal reported" refusal_reported "$out" +check "service container skipped: tendcf_service named" key_not_loaded "$out" tendcf_service +check "service container skipped: the pre_action did not run (first bundle gated)" no_actuation "$out" + +out="$work/xc-sim-partial.out" +sim_run runit-pair.json sim_interlock,sim_service_partial "$out" +check "half-loaded container: refusal reported" refusal_reported "$out" +check "half-loaded container: the entry the loader lost is named" \ + grep -q "key 'tendcf_service' loaded differently from .*: in the file only: \[demo-runit-two\]; loaded only: \[\]" "$out" +check "half-loaded container: the whole key is not reported as missing" \ + bash -c "! grep -q 'did not load data:variables' '$out'" +check "half-loaded container: nothing actuated (the loaded entry is not processed either)" \ + no_actuation "$out" + +out="$work/xc-sim-stray.out" +sim_run runit-pair.json sim_interlock,sim_service,sim_stray "$out" +check "extra loaded variable: refusal reported" refusal_reported "$out" +check "extra loaded variable: named" \ + grep -q "CFEngine loaded data:variables.stray, which is not a key under 'vars'" "$out" +check "extra loaded variable: nothing actuated" no_actuation "$out" + +out="$work/xc-sim-nofile.out" +sim_run none sim_stray "$out" +check "data loaded with no file: refusal reported" refusal_reported "$out" +check "data loaded with no file: reported" \ + grep -q "host_specific.json does not exist but CFEngine loaded data:variables.stray" "$out" +check "data loaded with no file: nothing actuated" no_actuation "$out" + if [ "$fail" != 0 ]; then - for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone; do + for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ + xc-malformed xc-nofile xc-novars xc-garbage xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile \ + sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue echo; echo "--- cf-agent output ($f) ---"; cat "$work/$f.out" done diff --git a/tests/policy/sim-loader.cf b/tests/policy/sim-loader.cf new file mode 100644 index 0000000..c85358d --- /dev/null +++ b/tests/policy/sim-loader.cf @@ -0,0 +1,46 @@ +# Test only (tests/policy/run.sh checks 9-10). Stands in for a CMDB loader that +# skipped, or half installed, a container of host_specific.json, which is the +# fork's behaviour on a bad `tendcf_interlock` entry (the Mac dev build rejects +# the whole `vars` instead, so it cannot be made to skip one container). Run +# cf-agent with --no-host-specific-data so this is the only thing installing +# data:variables.*, and list this file BEFORE tendcf_services.cf in `inputs`: +# the data must exist before tendcf_cmdb reads it. +# +# Classes (-D, default namespace) pick what to install, each from the file: +# sim_interlock install tendcf_interlock as the file has it +# sim_service install tendcf_service as the file has it +# sim_service_partial install tendcf_service with only `demo-runit` +# sim_stray also install data:variables.stray + +body file control +{ + namespace => "data"; +} + +bundle common sim_source +{ + vars: + "doc" + data => readjson("$(sys.workdir)/data/host_specific.json", 5242880), + ifvarclass => fileexists("$(sys.workdir)/data/host_specific.json"); +} + +bundle common variables +{ + vars: + "tendcf_interlock" + data => mergedata("data:sim_source.doc[vars][tendcf_interlock]"), + ifvarclass => "default:sim_interlock"; + + "tendcf_service" + data => mergedata("data:sim_source.doc[vars][tendcf_service]"), + ifvarclass => "default:sim_service"; + + "tendcf_service" + data => parsejson('{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}'), + ifvarclass => "default:sim_service_partial"; + + "stray" + string => "x", + ifvarclass => "default:sim_stray"; +} From 506e2f60a62e8d59be12155e71fbae1e9de12054 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 10:27:47 -0500 Subject: [PATCH 02/13] policy: compare the loaded CMDB containers' full content, refuse extra sections and non-identifier keys, clear preset id lists on refusal (PR 42 review: Astra A1/A2, adversary L1-L3) F1: tendcf_cmdb compares storejson() of each vars container with the loaded one; an uncomputable comparison is a difference. F2: any top-level key other than vars is refused (the loader also applies variables and classes); the two daemon-reload commands carry !tendcf_cmdb_refused. F3: a vars key that is not [A-Za-z0-9_]+ is refused; interlock_ids and service_ids are set to empty lists under the refusal. F4: the common-bundle ordering wording now states the vars/classes pre-evaluation guarantee with the pinned source, and that this is not a sandbox. Tests: run.sh checks 9-10 (variables collision, classes section, scoped preset, same-id changed value via the stand-in loader). Co-Authored-By: Claude Sonnet 5.5 --- .../tendcf-validate-proposal-2026-11-03.md | 53 +++-- policy/tendcf_services.cf | 205 ++++++++++++++---- .../policy/fixtures/cmdb-classes-section.json | 1 + tests/policy/fixtures/cmdb-scoped-preset.json | 1 + .../fixtures/cmdb-variables-collision.json | 1 + tests/policy/run.sh | 93 +++++++- tests/policy/sim-loader.cf | 13 ++ 7 files changed, 304 insertions(+), 63 deletions(-) create mode 100644 tests/policy/fixtures/cmdb-classes-section.json create mode 100644 tests/policy/fixtures/cmdb-scoped-preset.json create mode 100644 tests/policy/fixtures/cmdb-variables-collision.json diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index 1e6d78e..b518773 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1131,21 +1131,44 @@ service. That reproduces the fail-open before the fix. `bundle common tendcf_cmdb` in `policy/tendcf_services.cf` reads `$(sys.workdir)/data/host_specific.json` with `readjson` (5 MiB cap) and compares it with the data the loader installed. For every `vars` key it -requires `data:variables.` to exist and the key's entry ids to equal -the loaded container's indices (a half-loaded container is caught). It also -refuses a loaded `data:variables.*` that is not a `vars` key of the file, a -file that exists but cannot be parsed, and loaded data with no file. A missing -file with nothing loaded is the normal empty case and says nothing. On any -mismatch it defines the one class `tendcf_cmdb_refused`, and -`tendcf_interlocks` reports what differs once. Both bundles then see empty -`interlock_ids` and `service_ids`: no pre_action, no service, no tombstone. -It is a `bundle common`, so cf-agent evaluates it to completion before the -first agent bundle whatever `bundlesequence` says, which is what makes the -class final before any gate is read. `bundlesequence` guard classes in -`promises.cf` are not needed for this check. The fork-side all-or-nothing -loader (PR #41) and this check are independent: with the loader failing -closed, the file's keys are not loaded and the check refuses and reports -it, which is correct and louder. +requires `data:variables.` to exist, the key's entry ids to equal the +loaded container's indices (a half-loaded container is caught), and the whole +container to be identical: `storejson()` of the file's `vars[]` against +`storejson()` of `data:variables.`, string-equal, with a comparison that +cannot be computed counting as a difference. (Ids alone were the first +version; same ids with another value, which a `variables` section or a file +replaced between the loader's read and the policy's gives, passed it. Both +texts come from the one parser, so key order and number formatting cannot +differ on a consistent file; a `vars` value that is not an object, which the +loader turns into a string or an slist, is not comparable and is refused.) It +also refuses any top-level key of the file other than `vars` (the fork's +loader also applies `variables`, loaded after `vars`, and `classes`, which can +define `interlock_ok_*`; the projector emits neither, N-2), any `vars` key that +is not a plain identifier `[A-Za-z0-9_]+` (the loader reads a key as a variable +reference, `GetCMDBVariableRef`, so `default:tendcf_services.service_ids` +presets a variable of the policy's own bundles), a loaded `data:variables.*` +that is not a `vars` key of the file, a file that exists but cannot be +parsed, and loaded data with no file. A missing file with nothing loaded is +the normal empty case and says nothing. On any mismatch it defines the one +class `tendcf_cmdb_refused`, and `tendcf_interlocks` reports what differs +once. Both bundles then set `interlock_ids` and `service_ids` to explicitly +empty lists (an undefined list would leave a preset one in place), and the two +`daemon-reload` commands, which iterate no id, carry the refusal themselves: +no pre_action, no service, no tombstone. The vars and classes of a +`bundle common` are pre-evaluated before the vars of any non-common bundle, +whatever `bundlesequence` says (`PolicyResolve` and `BundleResolve`, +`libpromises/expand.c` lines 846-871 and 1145-1177 at the pinned fork commit +`b8abf28408b1`, [expand.c](https://github.com/djbclark/core/blob/b8abf28408b1b54e285d372a9bc5f2d850c960ad/libpromises/expand.c#L846)), +which is what makes the class final before any gate is read. That is a +guarantee about vars and classes only, not that every promise type of a common +bundle completes first, and the classes inside the check rely on their written +order within the one pass. `bundlesequence` guard classes in `promises.cf` are +not needed for this check. This is a consistency check, not a sandbox: a CMDB +author (root) already chooses each pre_action executable, `/bin/sh` included, +and the unit commands, so it does not stop a hostile but self-consistent file. +The fork-side all-or-nothing loader (PR #41) and this check are independent: +with the loader failing closed, the file's keys are not loaded and the check +refuses and reports it, which is correct and louder. Two findings from the implementation, both about the proof above. (1) The Mac dev build (`3.29.0a.086d27749`) does not carry the per-entry loader diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index 95a3da1..73db2c4 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -68,39 +68,78 @@ # negatively, on `!interlock_blocked_*`, and that class only exists for an # interlock that loaded). The check does not model WHY an entry was dropped. It # reads the file itself and compares it with what the loader installed: -# - for every top-level key under `vars`, `data:variables.` must exist; +# - the file's top-level keys must be exactly `vars`. The loader also applies +# a `variables` section (loaded AFTER `vars`, replacing a same-named +# variable) and a `classes` section (which can define `interlock_ok_*`); +# the projector emits neither (N-2, bin/projector.py check_projection), so +# any other top-level key is refused rather than compared; +# - every key under `vars` must be a plain identifier ([A-Za-z0-9_]+). The +# loader reads a key as a variable reference (cmdb.c GetCMDBVariableRef, +# VarRefParse): `a.b` lands in scope a, `ns:b.c` in another namespace, +# `a[i]` is an array element. Such a key can preset a variable of THIS +# policy (`default:tendcf_services.service_ids`), so it is refused; +# - for every key under `vars`, `data:variables.` must exist; # - per key, the set of entry ids in the file must equal the set of # indices of the loaded container (a half-loaded container is caught); +# - per key, the whole container the loader installed must equal the file's: +# storejson() of `doc[vars][]` against storejson() of +# `data:variables.`, string-equal (same parser and same bytes give the +# same text, key order and number formatting included; a key whose value is +# not an object is not comparable that way, because the loader turns a +# scalar into a string and a list of primitives into an slist, and is +# refused). Equal ids with changed values, as a `variables` entry or a +# replacement between the loader's read and ours would give, are caught; +# a comparison that cannot be computed counts as a difference; # - every `data:variables.*` the loader installed must be a key of `vars` -# in the file (it also catches a `variables` section in the file); +# in the file; # - a file that exists but cannot be parsed (or exceeds the loader's own # 5 MiB cap), and loaded data with no file at all, are mismatches. # A missing file with nothing loaded is the normal empty case: no class, no # report. A key the loader files elsewhere (`a.b`, `ns:b.c`) cannot match # `data:variables.`, so it reads as a mismatch: refused, never ignored. +# The data directory is `$(sys.workdir)/data` here; the loader uses +# GetDataDir() (CF_DATADIR, default /data). A build with another data +# dir would compare against the wrong file and mostly fail closed. # # On any mismatch the single class `tendcf_cmdb_refused` is defined and # NOTHING is actuated: no interlock pre_action, no service, no tombstone. The -# gate is one place per bundle: `interlock_ids` and `service_ids` are empty -# under the refusal, and every promise that acts iterates one of them -# (the two daemon-reload commands are guarded by classes only those promises -# set). A new actuating promise MUST iterate those ids or carry -# `!tendcf_cmdb_refused` itself. The mismatch is reported once, by -# tendcf_interlocks. +# gate is one place per bundle: `interlock_ids` and `service_ids` are set to +# empty lists under the refusal (explicitly, so a value a CMDB key preset in +# the bundle's scope is overwritten: variable redefinition is allowed unless a +# promise says `policy => "constant"`, verify_vars.c CollectConvergeVariable- +# Options), and every promise that acts iterates one of them. The two +# daemon-reload commands, which iterate no id, carry `!tendcf_cmdb_refused` +# themselves (a `classes` section could otherwise define their trigger +# classes; it is refused as well, but a refusal must not depend on that). A +# new actuating promise MUST iterate those ids or carry `!tendcf_cmdb_refused` +# itself. The mismatch is reported once, by tendcf_interlocks. # -# Order is load-bearing, as in the paragraph above: the check is a -# `bundle common`, which cf-agent evaluates to completion before any agent -# bundle, whatever `bundlesequence` says, so `tendcf_cmdb_refused` is final -# before the first gated class is read. It would not be inside an agent -# bundle that also gates (a class read before its definition is false and, -# once defined, never undone), and the check must not read a class it defines -# in a negative form for the same reason: only positive evidence defines a -# `tendcf_cmdb_problem_*` class. Proved on a real cf-agent by +# Order is load-bearing, as in the paragraph above. The fork evaluates the +# vars and the classes of every `bundle common` before the vars of any other +# bundle, independently of `bundlesequence`: PolicyResolve resolves the common +# bundles first and BundleResolve runs their vars, then classes, then vars +# again (libpromises/expand.c:846-871 and 1145-1177 at the commit pinned in +# overlay/fork.json, b8abf28408b1), so `tendcf_cmdb_refused` is final before +# the `!tendcf_cmdb_refused` on the agent bundles' `*_ids` is first read. That +# is a guarantee about vars and classes only, not that a common bundle's every +# promise type runs to completion first. Within the common bundle the classes +# run in one pass in written order: `tendcf_cmdb_refused` comes after every +# `tendcf_cmdb_problem_*` class, and `tendcf_cmdb_problem_content_*` reads the +# `tendcf_cmdb_json_equal_*` class defined just before it in the same section; +# do not reorder them. A class read before its definition is false and, once +# defined, never undone, so an agent bundle could not gate on a class it +# defines itself, and the check defines `tendcf_cmdb_problem_*` classes only +# from positive evidence (the content check reads its one negative form from +# the class defined immediately above it). Proved on a real cf-agent by # tests/policy/run.sh checks 9-10 (a pre_action must not run, a service must # not be processed). # -# What it cannot see: a `classes` section in the file (loaded as classes, not -# variables). The projector never emits one (N-2). +# What this is NOT: a sandbox. It is a consistency check between the file and +# what the loader installed. A CMDB author (root) already chooses each +# pre_action executable, `/bin/sh -c ...` included, and the unit commands, so +# it does not stop a hostile but self-consistent file; it stops a file and a +# loader that disagree, and (above) the sections and keys through which a file +# could change what the policy reads without showing up as a `vars` difference. bundle common tendcf_cmdb { @@ -122,6 +161,34 @@ bundle common tendcf_cmdb slist => { }, ifvarclass => not(isvariable("doc[vars]")); + # Top-level sections of the file. The loader applies `vars`, `variables` + # and `classes` (cmdb.c LoadCMDBData); the projector emits only `vars`, + # and anything else is refused (class `..._problem_sections`). A top-level + # array reads as indices "0", "1", ... here and is refused the same way. + "doc_sections" + slist => getindices("doc"), + ifvarclass => isvariable("doc"); + + "doc_sections" + slist => { }, + ifvarclass => not(isvariable("doc")); + + "allowed_sections" + slist => { "vars" }; + + "extra_sections" + slist => difference("doc_sections", "allowed_sections"); + + # Only a plain identifier is a `vars` key this policy will look up. The + # grep() pattern is anchored by CFEngine. Every other key is refused as a + # whole (`..._problem_badkey`) and is never used to build a variable name + # below. + "safe_keys" + slist => grep("[A-Za-z0-9_]+", "file_keys"); + + "bad_keys" + slist => difference("file_keys", "safe_keys"); + "loaded_refs" slist => variablesmatching("^data:variables\..*"); @@ -135,26 +202,36 @@ bundle common tendcf_cmdb "unlisted_keys" slist => difference("loaded_keys", "file_keys"); - "key_canon[$(file_keys)]" - string => canonify("$(file_keys)"); + "key_canon[$(safe_keys)]" + string => canonify("$(safe_keys)"); - "file_ids[$(file_keys)]" - slist => getindices("doc[vars][$(file_keys)]"); + "file_ids[$(safe_keys)]" + slist => getindices("doc[vars][$(safe_keys)]"); - "loaded_ids[$(file_keys)]" - slist => getindices("data:variables.$(file_keys)"); + "loaded_ids[$(safe_keys)]" + slist => getindices("data:variables.$(safe_keys)"); - "unloaded_ids[$(file_keys)]" - slist => difference("file_ids[$(file_keys)]", "loaded_ids[$(file_keys)]"); + "unloaded_ids[$(safe_keys)]" + slist => difference("file_ids[$(safe_keys)]", "loaded_ids[$(safe_keys)]"); - "unfiled_ids[$(file_keys)]" - slist => difference("loaded_ids[$(file_keys)]", "file_ids[$(file_keys)]"); + "unfiled_ids[$(safe_keys)]" + slist => difference("loaded_ids[$(safe_keys)]", "file_ids[$(safe_keys)]"); - "unloaded_text[$(file_keys)]" - string => join(", ", "unloaded_ids[$(file_keys)]"); + "unloaded_text[$(safe_keys)]" + string => join(", ", "unloaded_ids[$(safe_keys)]"); - "unfiled_text[$(file_keys)]" - string => join(", ", "unfiled_ids[$(file_keys)]"); + "unfiled_text[$(safe_keys)]" + string => join(", ", "unfiled_ids[$(safe_keys)]"); + + # The whole container, as JSON text, from the file and as installed. + # storejson() fails (the variable stays undefined) for anything that is + # not a container or list, and for a name that is not defined; the content + # class below treats an undefined side as a difference. + "file_json[$(safe_keys)]" + string => storejson("doc[vars][$(safe_keys)]"); + + "loaded_json[$(safe_keys)]" + string => storejson("data:variables.$(safe_keys)"); classes: "tendcf_cmdb_problem_unreadable" @@ -171,10 +248,29 @@ bundle common tendcf_cmdb "tendcf_cmdb_problem_unlisted" expression => isgreaterthan(length("unlisted_keys"), 0); - "tendcf_cmdb_problem_partial_$(key_canon[$(file_keys)])" - expression => and(isvariable("data:variables.$(file_keys)"), - or(isgreaterthan(length("unloaded_ids[$(file_keys)]"), 0), - isgreaterthan(length("unfiled_ids[$(file_keys)]"), 0))); + "tendcf_cmdb_problem_sections" + expression => isgreaterthan(length("extra_sections"), 0); + + "tendcf_cmdb_problem_badkey" + expression => isgreaterthan(length("bad_keys"), 0); + + "tendcf_cmdb_problem_partial_$(key_canon[$(safe_keys)])" + expression => and(isvariable("data:variables.$(safe_keys)"), + or(isgreaterthan(length("unloaded_ids[$(safe_keys)]"), 0), + isgreaterthan(length("unfiled_ids[$(safe_keys)]"), 0))); + + # Equal containers: defined only when both texts exist and are identical. + # The next promise reads this class negatively, which is sound only + # because it is evaluated after this one in the same pass (see the + # header): a loaded container that is not provably equal is a problem, + # whether the texts differ or one of them could not be computed. + "tendcf_cmdb_json_equal_$(key_canon[$(safe_keys)])" + expression => strcmp("$(file_json[$(safe_keys)])", + "$(loaded_json[$(safe_keys)])"); + + "tendcf_cmdb_problem_content_$(key_canon[$(safe_keys)])" + expression => and(isvariable("data:variables.$(safe_keys)"), + not("tendcf_cmdb_json_equal_$(key_canon[$(safe_keys)])")); "tendcf_cmdb_refused" expression => classmatch("tendcf_cmdb_problem_.*"); @@ -184,11 +280,18 @@ bundle agent tendcf_interlocks { vars: # Empty under a CMDB mismatch (see `tendcf_cmdb`): with no ids no - # pre_action command is even built. + # pre_action command is even built. Set explicitly, not left undefined: + # a `vars` key such as "default:tendcf_interlocks.interlock_ids" would + # have preset the list before the policy loaded, and a skipped promise + # does not remove it. "interlock_ids" slist => getindices("data:variables.tendcf_interlock"), ifvarclass => "!tendcf_cmdb_refused"; + "interlock_ids" + slist => { }, + ifvarclass => "tendcf_cmdb_refused"; + "interlock_canon[$(interlock_ids)]" string => canonify("$(interlock_ids)"); @@ -280,22 +383,40 @@ bundle agent tendcf_interlocks "tendcf_cmdb_problem_unlisted":: "tendcf: CMDB cross-check: CFEngine loaded data:variables.$(tendcf_cmdb.unlisted_keys), which is not a key under 'vars' in $(tendcf_cmdb.file)"; - "tendcf_cmdb_problem_partial_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.file_keys)])":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.file_keys)' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.file_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.file_keys)])]"; + "tendcf_cmdb_problem_partial_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.safe_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.safe_keys)])]"; + + "tendcf_cmdb_problem_content_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content differs between $(tendcf_cmdb.file) and the data CFEngine loaded (or the two could not be compared)"; + + "tendcf_cmdb_problem_sections":: + "tendcf: CMDB cross-check: $(tendcf_cmdb.file) has top-level section '$(tendcf_cmdb.extra_sections)' besides 'vars'; only 'vars' is accepted (the loader would also apply 'variables' and 'classes')"; + + "tendcf_cmdb_problem_badkey":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.bad_keys)' under 'vars' in $(tendcf_cmdb.file) is not a plain identifier ([A-Za-z0-9_]+); the loader would read it as a variable reference"; } bundle agent tendcf_services { vars: - # Empty under a CMDB mismatch, see tendcf_interlocks and `tendcf_cmdb`. + # Empty under a CMDB mismatch, see tendcf_interlocks and `tendcf_cmdb` + # (set explicitly for the same reason as there). "service_ids" slist => getindices("data:variables.tendcf_service"), ifvarclass => "!tendcf_cmdb_refused"; + "service_ids" + slist => { }, + ifvarclass => "tendcf_cmdb_refused"; + "interlock_ids" slist => getindices("data:variables.tendcf_interlock"), ifvarclass => "!tendcf_cmdb_refused"; + "interlock_ids" + slist => { }, + ifvarclass => "tendcf_cmdb_refused"; + "service_canon[$(service_ids)]" string => canonify("$(service_ids)"); @@ -509,7 +630,7 @@ bundle agent tendcf_services # unit is on disk before the manager is told about it. "/usr/bin/systemctl" arglist => { "daemon-reload" }, - ifvarclass => "tendcf_systemd_reload"; + ifvarclass => "tendcf_systemd_reload.!tendcf_cmdb_refused"; "/usr/bin/systemctl" arglist => { "enable", "$(service_ids)" }, @@ -537,7 +658,7 @@ bundle agent tendcf_services # so a run that already reloaded for a render still reloads again. "/usr/bin/systemctl" arglist => { "--quiet", "daemon-reload" }, - ifvarclass => "tendcf_systemd_unit_removed"; + ifvarclass => "tendcf_systemd_unit_removed.!tendcf_cmdb_refused"; reports: "interlock_blocked_$(interlock_bundle_canon[$(interlock_ids)])":: diff --git a/tests/policy/fixtures/cmdb-classes-section.json b/tests/policy/fixtures/cmdb-classes-section.json new file mode 100644 index 0000000..a37d1b1 --- /dev/null +++ b/tests/policy/fixtures/cmdb-classes-section.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}},"classes":{"interlock_ok_demo_check":"any::"}} diff --git a/tests/policy/fixtures/cmdb-scoped-preset.json b/tests/policy/fixtures/cmdb-scoped-preset.json new file mode 100644 index 0000000..776dde4 --- /dev/null +++ b/tests/policy/fixtures/cmdb-scoped-preset.json @@ -0,0 +1 @@ +{"vars":{"default:tendcf_interlocks.interlock_ids":["demo-check"],"default:tendcf_services.interlock_ids":["demo-check"],"default:tendcf_services.service_ids":["demo-runit"],"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}}} diff --git a/tests/policy/fixtures/cmdb-variables-collision.json b/tests/policy/fixtures/cmdb-variables-collision.json new file mode 100644 index 0000000..23317a6 --- /dev/null +++ b/tests/policy/fixtures/cmdb-variables-collision.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{}},"variables":{"tendcf_interlock":{"value":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}}}}} diff --git a/tests/policy/run.sh b/tests/policy/run.sh index 4b89583..a8606db 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -55,10 +55,17 @@ # policy/tendcf_services.cf, proposal 2026-11-03 section 13 option 3): a # file the loader cannot install whole is refused and reported and # actuates nothing; the consistent files of checks 2-4 raise no report; no -# file at all, and a file with no vars, are the normal empty case. NB: the -# fork's loader skips a bad entry and installs the rest, but the Mac dev -# build rejects the whole `vars` instead (the same fixture refuses on both, -# as "nothing loaded"), so only the fork's CI legs see the per-entry skip. +# file at all, and a file with no vars, are the normal empty case. Also +# refused and reported, each actuating nothing: a `variables` section that +# replaces a container under the same ids with another command (the +# comparison is of the whole container, not of key names and ids), a +# `classes` section, and a `vars` key that is not a plain identifier, which +# the loader would file under another scope (a preset of the policy's own +# id lists). Every file of checks 2-8 and 6 is also a no-report control. +# NB: the fork's loader skips a bad entry and installs the rest, but the +# Mac dev build rejects the whole `vars` instead (the same fixture refuses +# on both, as "nothing loaded"), so only the fork's CI legs see the +# per-entry skip. # 10. the same cross-check against a loader that skips ONE container, which # the Mac dev build cannot do: tests/policy/sim-loader.cf installs # data:variables.* in the loader's place (cf-agent --no-host-specific-data) @@ -68,7 +75,9 @@ # A skipped interlock container with the services loaded is the 2026-11-02 # fail-open; a skipped service container with the interlock loaded shows # the first bundle (tendcf_interlocks, which runs pre_action commands) is -# gated too. Under TENDCF_TEST_SYSTEMD=1 the same stand-in is run against +# gated too. Same ids with a changed value (a service's command, or an +# interlock's pre_action) are refused as a content difference, which no +# key-and-id comparison could see. Under TENDCF_TEST_SYSTEMD=1 the same stand-in is run against # the real manager (6b): no unit rendered, a tombstone leaves the unit alone. # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist @@ -547,6 +556,54 @@ check "unparseable file: reported as unreadable" \ check "unparseable file: refusal reported once" refusal_once "$out" check "unparseable file: nothing actuated" no_actuation "$out" +# Consistent files from the other checks (8, 8c, 8d, and the systemd lifecycle +# when it ran): the whole-container comparison must not refuse a normal file, so +# key order and number formatting do not make a false refusal. +for f in noshell-or noshell-semi argv-alphabet argv-meta relative sysd1 sysd2 sysd3 sysd4 sysd5; do + [ -f "$work/$f.out" ] || continue + check "consistent file of the $f run: no cross-check report" no_crosscheck "$work/$f.out" +done + +# A `variables` section replaces `vars.tendcf_interlock` under the same id with +# a pre_action that passes (the file's own vars say /usr/bin/false). The loader +# applies `variables` after `vars` (cmdb.c LoadCMDBData), so the ids match and +# only the content differs. +cp "$repo/tests/policy/fixtures/cmdb-variables-collision.json" "$work/data/host_specific.json" +out="$work/xc-collision.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "variables-section collision: refusal reported" refusal_reported "$out" +check "variables-section collision: the content difference is named" \ + grep -q "key 'tendcf_interlock' content differs between" "$out" +check "variables-section collision: not an id difference" \ + bash -c "! grep -q 'loaded differently' '$out'" +check "variables-section collision: the section is named" \ + grep -q "has top-level section 'variables' besides 'vars'" "$out" +check "variables-section collision: refusal reported once" refusal_once "$out" +check "variables-section collision: the replacement pre_action did not run" no_actuation "$out" + +# A `classes` section can define interlock_ok_demo_check, which would unblock the +# bundle whatever the pre_action returns (it is /usr/bin/false here). +cp "$repo/tests/policy/fixtures/cmdb-classes-section.json" "$work/data/host_specific.json" +out="$work/xc-classes.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "classes section: refusal reported" refusal_reported "$out" +check "classes section: the section is named" \ + grep -q "has top-level section 'classes' besides 'vars'" "$out" +check "classes section: nothing actuated (no pre_action, no service processed)" no_actuation "$out" + +# `vars` keys that name a variable of the policy's own bundles preset the id +# lists before the policy loads; a refusal must overwrite them, not leave them. +cp "$repo/tests/policy/fixtures/cmdb-scoped-preset.json" "$work/data/host_specific.json" +out="$work/xc-preset.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "scoped-key preset: refusal reported" refusal_reported "$out" +for k in default:tendcf_interlocks.interlock_ids default:tendcf_services.interlock_ids default:tendcf_services.service_ids; do + check "scoped-key preset: '$k' refused as not a plain identifier" \ + grep -q "key '$k' under 'vars' in .* is not a plain identifier" "$out" +done +check "scoped-key preset: refusal reported once" refusal_once "$out" +check "scoped-key preset: the preset id lists did not actuate" no_actuation "$out" + echo "# 10. CMDB cross-check, a loader that skips one container (tests/policy/sim-loader.cf)" sim_run() { # sim_run FIXTURE|none CLASSES OUT if [ "$1" = none ]; then rm -f "$work/data/host_specific.json" @@ -594,6 +651,29 @@ check "extra loaded variable: named" \ grep -q "CFEngine loaded data:variables.stray, which is not a key under 'vars'" "$out" check "extra loaded variable: nothing actuated" no_actuation "$out" +# Same ids, a changed value: what a loader that replaced an entry's value (or a +# file replaced between the loader's read and the policy's) would leave. Ids and +# keys match, so only the whole-container comparison can see it. +out="$work/xc-sim-changed-service.out" +sim_run flavour-refused.json sim_interlock,sim_service_changed "$out" +check "service value changed under the same id: refusal reported" refusal_reported "$out" +check "service value changed under the same id: the container is named" \ + grep -q "key 'tendcf_service' content differs between" "$out" +check "service value changed under the same id: not an id difference" \ + bash -c "! grep -q 'loaded differently' '$out'" +check "service value changed under the same id: tendcf_interlock not blamed" \ + bash -c "! grep -q \"key 'tendcf_interlock'\" '$out'" +check "service value changed under the same id: nothing actuated" no_actuation "$out" + +out="$work/xc-sim-changed-interlock.out" +sim_run interlock-refused.json sim_interlock_changed,sim_service "$out" +check "interlock value changed under the same id: refusal reported" refusal_reported "$out" +check "interlock value changed under the same id: the container is named" \ + grep -q "key 'tendcf_interlock' content differs between" "$out" +check "interlock value changed under the same id: tendcf_service not blamed" \ + bash -c "! grep -q \"key 'tendcf_service'\" '$out'" +check "interlock value changed under the same id: the passing pre_action did not run" no_actuation "$out" + out="$work/xc-sim-nofile.out" sim_run none sim_stray "$out" check "data loaded with no file: refusal reported" refusal_reported "$out" @@ -603,7 +683,8 @@ check "data loaded with no file: nothing actuated" no_actuation "$out" if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset \ + xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue echo; echo "--- cf-agent output ($f) ---"; cat "$work/$f.out" diff --git a/tests/policy/sim-loader.cf b/tests/policy/sim-loader.cf index c85358d..b35a30b 100644 --- a/tests/policy/sim-loader.cf +++ b/tests/policy/sim-loader.cf @@ -10,6 +10,11 @@ # sim_interlock install tendcf_interlock as the file has it # sim_service install tendcf_service as the file has it # sim_service_partial install tendcf_service with only `demo-runit` +# sim_service_changed install tendcf_service with `demo-runit` under the same +# id but another command (/bin/sleep 2, not 1) +# sim_interlock_changed install tendcf_interlock with `demo-check` under the +# same id but pre_action /usr/bin/true (a loader that +# replaced the value of an entry it kept) # sim_stray also install data:variables.stray body file control @@ -40,6 +45,14 @@ bundle common variables data => parsejson('{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}'), ifvarclass => "default:sim_service_partial"; + "tendcf_service" + data => parsejson('{"demo-runit":{"bundle":"demo","command":["/bin/sleep","2"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}'), + ifvarclass => "default:sim_service_changed"; + + "tendcf_interlock" + data => parsejson('{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}}'), + ifvarclass => "default:sim_interlock_changed"; + "stray" string => "x", ifvarclass => "default:sim_stray"; From d1b0c300bddcc4eb7e8715be5b6dc73449c20f6f Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 10:41:17 -0500 Subject: [PATCH 03/13] tests: prove the scoped-key preset is real (loader control) and that an uncomparable vars value is refused Co-Authored-By: Claude Sonnet 5.5 --- tests/policy/run.sh | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/policy/run.sh b/tests/policy/run.sh index a8606db..b9bafca 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -61,7 +61,9 @@ # comparison is of the whole container, not of key names and ids), a # `classes` section, and a `vars` key that is not a plain identifier, which # the loader would file under another scope (a preset of the policy's own -# id lists). Every file of checks 2-8 and 6 is also a no-report control. +# id lists), and a `vars` value that is not an object (not comparable as +# JSON text with the file's). Every file of checks 2-8 and 6 is also a +# no-report control. # NB: the fork's loader skips a bad entry and installs the rest, but the # Mac dev build rejects the whole `vars` instead (the same fixture refuses # on both, as "nothing loaded"), so only the fork's CI legs see the @@ -603,6 +605,27 @@ for k in default:tendcf_interlocks.interlock_ids default:tendcf_services.interlo done check "scoped-key preset: refusal reported once" refusal_once "$out" check "scoped-key preset: the preset id lists did not actuate" no_actuation "$out" +# The control that gives the case above its teeth: with no tendcf policy at all, +# the loader really does preset the policy's own variable from such a key (so the +# refusal had to overwrite it, not merely find it absent). +printf 'bundle agent main\n{\n}\n' > "$work/inputs/empty.cf" +"$cf_promises" --show-vars -f "$work/inputs/empty.cf" >"$work/xc-preset-vars.out" 2>&1 || true +check "scoped-key preset (control): the loader installed default:tendcf_services.service_ids" \ + bash -c "grep 'default:tendcf_services.service_ids' '$work/xc-preset-vars.out' | grep -q demo-runit" + +# A `vars` value that is not an object (the loader turns a string into a scalar +# and a list of primitives into an slist) cannot be compared as JSON text with +# the file's: not computable, so refused, and named. +printf '%s\n' '{"vars":{"tendcf_interlock":{},"tendcf_service":{},"scalar":"x","plain_list":["a","b"]}}' >"$work/data/host_specific.json" +out="$work/xc-uncomparable.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "uncomparable vars values: refusal reported" refusal_reported "$out" +for k in scalar plain_list; do + check "uncomparable vars value '$k': named as a content difference" \ + grep -q "key '$k' content differs between" "$out" +done +check "uncomparable vars values: the object containers are not blamed" \ + bash -c "! grep -Eq \"key 'tendcf_(interlock|service)' content differs\" '$out'" echo "# 10. CMDB cross-check, a loader that skips one container (tests/policy/sim-loader.cf)" sim_run() { # sim_run FIXTURE|none CLASSES OUT @@ -683,7 +706,7 @@ check "data loaded with no file: nothing actuated" no_actuation "$out" if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable \ xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue From 56cf91f9e3e510dcc93ff86ed69ef4bb17e746b4 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 10:47:06 -0500 Subject: [PATCH 04/13] tests: a list of numbers is the uncomparable vars case (a list of strings round-trips equal); fix the wording Co-Authored-By: Claude Sonnet 5.5 --- .../tendcf-validate-proposal-2026-11-03.md | 5 +++-- policy/tendcf_services.cf | 9 +++++---- tests/policy/run.sh | 10 ++++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index b518773..061502d 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1139,8 +1139,9 @@ cannot be computed counting as a difference. (Ids alone were the first version; same ids with another value, which a `variables` section or a file replaced between the loader's read and the policy's gives, passed it. Both texts come from the one parser, so key order and number formatting cannot -differ on a consistent file; a `vars` value that is not an object, which the -loader turns into a string or an slist, is not comparable and is refused.) It +differ on a consistent file; a `vars` value that is not an object is refused when it reads back +differently: the loader installs a scalar as a string, which `storejson()` +cannot read, and a list of numbers as an slist of strings.) It also refuses any top-level key of the file other than `vars` (the fork's loader also applies `variables`, loaded after `vars`, and `classes`, which can define `interlock_ok_*`; the projector emits neither, N-2), any `vars` key that diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index 73db2c4..a7c3b0a 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -84,10 +84,11 @@ # - per key, the whole container the loader installed must equal the file's: # storejson() of `doc[vars][]` against storejson() of # `data:variables.`, string-equal (same parser and same bytes give the -# same text, key order and number formatting included; a key whose value is -# not an object is not comparable that way, because the loader turns a -# scalar into a string and a list of primitives into an slist, and is -# refused). Equal ids with changed values, as a `variables` entry or a +# same text, key order and number formatting included). A value that is not +# an object is refused when it reads back differently: the loader installs +# a scalar as a string (storejson() fails) and a list of primitives as an +# slist of strings (numbers read back as strings). Equal ids with changed +# values, as a `variables` entry or a # replacement between the loader's read and ours would give, are caught; # a comparison that cannot be computed counts as a difference; # - every `data:variables.*` the loader installed must be a key of `vars` diff --git a/tests/policy/run.sh b/tests/policy/run.sh index b9bafca..8769bb9 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -613,10 +613,12 @@ printf 'bundle agent main\n{\n}\n' > "$work/inputs/empty.cf" check "scoped-key preset (control): the loader installed default:tendcf_services.service_ids" \ bash -c "grep 'default:tendcf_services.service_ids' '$work/xc-preset-vars.out' | grep -q demo-runit" -# A `vars` value that is not an object (the loader turns a string into a scalar -# and a list of primitives into an slist) cannot be compared as JSON text with -# the file's: not computable, so refused, and named. -printf '%s\n' '{"vars":{"tendcf_interlock":{},"tendcf_service":{},"scalar":"x","plain_list":["a","b"]}}' >"$work/data/host_specific.json" +# A `vars` value that is not an object is installed as a scalar (not +# comparable: storejson() fails) or, for a list of primitives, as an slist of +# strings, so numbers read back as "1","2" and differ from the file's [1,2]: +# either way a difference, refused and named. (A list of strings would compare +# equal and pass, which is correct.) +printf '%s\n' '{"vars":{"tendcf_interlock":{},"tendcf_service":{},"scalar":"x","plain_list":[1,2]}}' >"$work/data/host_specific.json" out="$work/xc-uncomparable.out" "$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true check "uncomparable vars values: refusal reported" refusal_reported "$out" From 8ee5bba73e7c3fdf180b8f0dbe66179eff51dab1 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 11:18:20 -0500 Subject: [PATCH 05/13] tests: reproduce the round-2 bypasses on the unfixed policy (checker presets, hidden classes section, augments) and count tendcf_cmdb evaluations Deliberately red against 56cf91f's policy: the new checks 9 cases expect a refusal the policy cannot give yet. Check 11 only observes how often the engine evaluates tendcf_cmdb. Co-Authored-By: Claude Sonnet 5.5 --- .../fixtures/cmdb-checker-preset-keys.json | 1 + .../policy/fixtures/cmdb-checker-preset.json | 1 + .../policy/fixtures/cmdb-classes-hidden.json | 1 + tests/policy/probe-mutate.cf | 14 +++ tests/policy/run.sh | 85 ++++++++++++++++++- 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/policy/fixtures/cmdb-checker-preset-keys.json create mode 100644 tests/policy/fixtures/cmdb-checker-preset.json create mode 100644 tests/policy/fixtures/cmdb-classes-hidden.json create mode 100644 tests/policy/probe-mutate.cf diff --git a/tests/policy/fixtures/cmdb-checker-preset-keys.json b/tests/policy/fixtures/cmdb-checker-preset-keys.json new file mode 100644 index 0000000..c393ed8 --- /dev/null +++ b/tests/policy/fixtures/cmdb-checker-preset-keys.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}},"default:tendcf_services.service_ids":["demo-runit"],"default:tendcf_cmdb.bad_keys":[],"default:tendcf_cmdb.missing_keys":[]}} diff --git a/tests/policy/fixtures/cmdb-checker-preset.json b/tests/policy/fixtures/cmdb-checker-preset.json new file mode 100644 index 0000000..55279d3 --- /dev/null +++ b/tests/policy/fixtures/cmdb-checker-preset.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}},"default:tendcf_cmdb.doc":{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}}}},"default:tendcf_services.interlock_ids":{}}} diff --git a/tests/policy/fixtures/cmdb-classes-hidden.json b/tests/policy/fixtures/cmdb-classes-hidden.json new file mode 100644 index 0000000..deb1a29 --- /dev/null +++ b/tests/policy/fixtures/cmdb-classes-hidden.json @@ -0,0 +1 @@ +{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/false"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{"demo-runit":{"bundle":"demo","command":["/bin/sleep","1"],"run_as":"nobody","state":"present","unit":{"runit":{"enabled":true}},"working_dir":"/"}},"default:tendcf_cmdb.extra_sections":[],"default:tendcf_cmdb.bad_keys":[],"default:tendcf_cmdb.missing_keys":[]},"classes":{"interlock_ok_demo_check":"any::"}} diff --git a/tests/policy/probe-mutate.cf b/tests/policy/probe-mutate.cf new file mode 100644 index 0000000..917202f --- /dev/null +++ b/tests/policy/probe-mutate.cf @@ -0,0 +1,14 @@ +# Test only (tests/policy/run.sh check 11). A `bundle common` listed AFTER +# policy/tendcf_services.cf in `inputs`, so PolicyResolve resolves it after +# tendcf_cmdb has finished its first vars, classes and vars passes. Its one vars +# promise runs a script that replaces host_specific.json with a file that has the +# same keys and entry ids but another pre_action command, i.e. a publication +# between two evaluations of tendcf_cmdb. What the policy does after that shows +# whether, and when, the engine evaluates tendcf_cmdb again. + +bundle common tendcf_probe_mutate +{ + vars: + "ran" + string => execresult("$(sys.workdir)/probe-mutate.sh", "noshell"); +} diff --git a/tests/policy/run.sh b/tests/policy/run.sh index 8769bb9..9ebbd90 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -156,6 +156,8 @@ refusal_reported() { grep -q "tendcf: CMDB cross-check failed: refusing to actua refusal_once() { [ "$(grep -c "CMDB cross-check failed" "$1")" = 1 ]; } no_crosscheck() { ! grep -q "CMDB cross-check" "$1"; } no_actuation() { ! grep -Eq "Executing|Created file|launchctl|systemctl|tendcf: (service|interlock) '" "$1"; } +foreign_var_reported() { grep -q "tendcf: CMDB cross-check: the CMDB or an augments file installed a variable outside" "$1"; } +foreign_class_reported() { grep -q "tendcf: CMDB cross-check: the CMDB or an augments file defined a class" "$1"; } key_not_loaded() { grep -q "key '$2' is under 'vars' in .* but CFEngine did not load data:variables.$2" "$1"; } echo "# 1. wiring: cf-promises with examples/host_specific.json" @@ -629,6 +631,45 @@ done check "uncomparable vars values: the object containers are not blamed" \ bash -c "! grep -Eq \"key 'tendcf_(interlock|service)' content differs\" '$out'" +# A `vars` key can preset any variable of the policy, the checker's own included +# (adversary M1): a vars promise whose rvalue is a function call is skipped when +# the variable already exists (verify_vars.c, "Already did this"), so a preset +# `doc`, `bad_keys` or `missing_keys` of tendcf_cmdb, or an id list of the gates, +# stays. The policy refuses any CMDB-installed variable that is not +# data:variables. and any CMDB-installed class, read from the loaded +# state, so there is no variable of the checker's to preset. +for fx in checker-preset checker-preset-keys classes-hidden; do + cp "$repo/tests/policy/fixtures/cmdb-$fx.json" "$work/data/host_specific.json" + out="$work/xc-$fx.out" + "$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true + check "$fx: refusal reported" refusal_reported "$out" + check "$fx: the foreign variable is reported" foreign_var_reported "$out" + check "$fx: refusal reported once" refusal_once "$out" + check "$fx: nothing actuated (no pre_action, no service processed)" no_actuation "$out" +done +check "classes-hidden: the foreign class is reported" foreign_class_reported "$work/xc-classes-hidden.out" + +# Augments (def.json in inputs/) are loaded after the CMDB and can define classes +# and variables just the same. tendcf ships none, so any is refused. +cp "$repo/tests/policy/fixtures/interlock-refused.json" "$work/data/host_specific.json" +printf '%s\n' '{"classes":{"interlock_ok_demo_check":["any"]},"vars":{"probe":"x"}}' >"$work/inputs/def.json" +out="$work/xc-augments.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +rm -f "$work/inputs/def.json" +check "augments: refusal reported" refusal_reported "$out" +check "augments: the foreign variable is reported" foreign_var_reported "$out" +check "augments: the foreign class is reported" foreign_class_reported "$out" +check "augments: nothing actuated (the class would have unblocked the bundle)" no_actuation "$out" + +# Positive control (review B3): a `vars` container that is a list of strings +# round-trips through the loader's slist, so it is accepted and the run actuates. +printf '%s\n' '{"vars":{"tendcf_interlock":{"demo-check":{"blocks":"enclosing-bundle","bundle":"demo","defines_class":"demo_ok","pre_action":{"command":["/usr/bin/true"],"expect_exit":0,"timeout_seconds":5},"report":true,"state":"present"}},"tendcf_service":{},"plain_strings":["a","b"]}}' >"$work/data/host_specific.json" +out="$work/xc-strlist.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "list of strings in vars: no cross-check report" no_crosscheck "$out" +check "list of strings in vars: actuates normally (the pre_action ran)" \ + grep -q "Executing .*'/usr/bin/true'" "$out" + echo "# 10. CMDB cross-check, a loader that skips one container (tests/policy/sim-loader.cf)" sim_run() { # sim_run FIXTURE|none CLASSES OUT if [ "$1" = none ]; then rm -f "$work/data/host_specific.json" @@ -706,9 +747,51 @@ check "data loaded with no file: reported" \ grep -q "host_specific.json does not exist but CFEngine loaded data:variables.stray" "$out" check "data loaded with no file: nothing actuated" no_actuation "$out" +echo "# 11. when does the engine evaluate tendcf_cmdb? (tests/policy/probe-mutate.cf)" +# The file is consistent with what the loader installed (A). A common bundle +# resolved after tendcf_cmdb replaces it with B: same keys and ids, another +# pre_action command. If the engine evaluated tendcf_cmdb's variables or classes +# again, the policy would read B; it must then refuse everything. If it does not, +# the pre_action of A runs, never B's. Either way B is never acted on. The debug +# log (-d) counts the evaluations, so a later engine change shows up here. +cat > "$work/inputs/probe-entry.cf" < { "$repo/policy/tendcf_services.cf", "$repo/tests/policy/probe-mutate.cf" }; + bundlesequence => { "tendcf_interlocks", "tendcf_services" }; +} +EOF2 +cat > "$work/probe-mutate.sh" <> "$work/probe-count" +cp "$work/probe-B.json" "$work/data/host_specific.json" +EOF2 +chmod 700 "$work/probe-mutate.sh" +cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_specific.json" +probe_a="$(cat "$repo/tests/policy/fixtures/interlock-satisfied.json")" +printf '%s\n' "${probe_a//\"\/usr\/bin\/true\"/\"\/usr\/bin\/false\"}" > "$work/probe-B.json" +rm -f "$work/probe-count" +out="$work/xc-probe.out" +"$cf_agent" -K -I -d -f "$work/inputs/probe-entry.cf" >"$out" 2>&1 || true +n_doc="$(grep -c 'Evaluating vars promise: doc$' "$out" || true)" +n_json="$(grep -c 'Evaluating vars promise: file_json\[' "$out" || true)" +n_cls="$(grep -c 'Evaluating classes promise: tendcf_cmdb_refused$' "$out" || true)" +n_res="$(grep -c "Resolving classes and variables in 'bundle common tendcf_cmdb'" "$out" || true)" +n_mut="$(wc -l < "$work/probe-count" 2>/dev/null || echo 0)" +echo "info: tendcf_cmdb resolved $n_res times; vars 'doc' visited $n_doc times, 'file_json[..]' $n_json times; class 'tendcf_cmdb_refused' visited $n_cls times; mutation script ran $n_mut times" +check "probe: the mutation script ran" [ "$n_mut" -ge 1 ] +if refusal_reported "$out"; then + echo "info: the engine evaluated tendcf_cmdb again and the policy read the replaced file" + check "probe: refused after the replacement, nothing actuated" no_actuation "$out" +else + echo "info: the policy did not read the replaced file (tendcf_cmdb is not evaluated again)" + check "probe: not refused, the loaded pre_action (true) ran and the replaced one (false) did not" \ + bash -c "grep -q \"Executing .*'/usr/bin/true'\" '$out' && ! grep -q \"Executing .*'/usr/bin/false'\" '$out'" +fi + if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist \ xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue From d288c81078f40819ce78c5c9b9459b5579478bdf Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 11:21:17 -0500 Subject: [PATCH 06/13] policy: refuse any CMDB- or augments-installed variable or class outside data:variables., define problem classes positively from the current comparison, sanitise reported text; installer writes data by atomic rename PR 42 round 2 (Astra A1/A2, adversary M1/L1/L2/I1). Closes the preset of the checker's own variables and of the gates' id lists (a vars promise with a function-call rvalue is skipped when the variable exists), replaces the sticky tendcf_cmdb_json_equal_* negation with a differs class and an uncomparable class, and prints file-derived text only in a sanitised, capped form. Docs and header corrected to what the source supports. Co-Authored-By: Claude Sonnet 5.5 --- deploy/linux/tendcf-install.sh | 10 +- .../tendcf-validate-proposal-2026-11-03.md | 52 +++- policy/tendcf_services.cf | 242 +++++++++++++----- tests/policy/run.sh | 28 +- 4 files changed, 254 insertions(+), 78 deletions(-) diff --git a/deploy/linux/tendcf-install.sh b/deploy/linux/tendcf-install.sh index 2555dc7..158de91 100755 --- a/deploy/linux/tendcf-install.sh +++ b/deploy/linux/tendcf-install.sh @@ -148,7 +148,15 @@ put_data() { # put_data FILE backup="$data.previous" cp -p "$data" "$backup" fi - install -m 0600 -o root -g root "$src" "$data" + # Write beside the live file, then rename over it: the agent (cf-execd runs + # it on a schedule) never reads a half-written host_specific.json, and the + # loader and the policy's cross-check cannot see two different halves. + local tmp="$data.new" + if ! install -m 0600 -o root -g root "$src" "$tmp"; then + rm -f "$tmp" + die "could not write $tmp" + fi + mv -f "$tmp" "$data" if ! "$cf_promises" -f "$entry" >/dev/null; then if [ -n "$backup" ]; then mv -f "$backup" "$data"; else rm -f "$data"; fi die "cf-promises rejected $src; previous data restored" diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index 061502d..730a167 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1135,13 +1135,14 @@ requires `data:variables.` to exist, the key's entry ids to equal the loaded container's indices (a half-loaded container is caught), and the whole container to be identical: `storejson()` of the file's `vars[]` against `storejson()` of `data:variables.`, string-equal, with a comparison that -cannot be computed counting as a difference. (Ids alone were the first -version; same ids with another value, which a `variables` section or a file -replaced between the loader's read and the policy's gives, passed it. Both -texts come from the one parser, so key order and number formatting cannot -differ on a consistent file; a `vars` value that is not an object is refused when it reads back -differently: the loader installs a scalar as a string, which `storejson()` -cannot read, and a list of numbers as an slist of strings.) It +cannot be computed counted as a problem of its own (not as the absence of a +difference). (Ids alone were the first +version; same ids with another value, which a `variables` section gives, passed +it. Both texts come from the one parser, so key order and number formatting +cannot differ on a consistent file; a `vars` value that is not an object is +refused when it reads back differently: the loader installs a scalar as a +string, which `storejson()` cannot read, and a list of numbers as an slist of +strings, while a list of strings reads back equal and is accepted.) It also refuses any top-level key of the file other than `vars` (the fork's loader also applies `variables`, loaded after `vars`, and `classes`, which can define `interlock_ok_*`; the projector emits neither, N-2), any `vars` key that @@ -1150,18 +1151,47 @@ reference, `GetCMDBVariableRef`, so `default:tendcf_services.service_ids` presets a variable of the policy's own bundles), a loaded `data:variables.*` that is not a `vars` key of the file, a file that exists but cannot be parsed, and loaded data with no file. A missing file with nothing loaded is -the normal empty case and says nothing. On any mismatch it defines the one +the normal empty case and says nothing. + +Round 2 of the review (Astra A1, adversary M1) added the loaded-state check. +A preset reaches the checker itself: a vars promise whose rvalue is a function +call (`readjson`, `getindices`, `difference`, every variable the checker +computes and the non-refused id lists) is skipped when the variable already +exists (`verify_vars.c`, "Already did this"; only a promise with a literal +rvalue redefines it). So `default:tendcf_cmdb.doc`, +`default:tendcf_cmdb.bad_keys` or `default:tendcf_services.interlock_ids` in +the file keeps its value, and the check passes while the loaded interlock is +ignored. The policy therefore reads the loaded state directly, in class +expressions that call the functions inline (no variable to preset): any +variable tagged `source=cmdb` or `source=augments_file` outside +`data:variables.` (`variablesmatching`) and any class with either +tag (`classmatch`) is refused. tendcf ships no `def.json`, so augments are +refused too (a host that needs one must be exempted in the policy, on +purpose). The content comparison no longer reads the negation of a class set +once and never cleared (`tendcf_cmdb_json_equal_*`, which a later mismatch +could not unset); `tendcf_cmdb_problem_differs_` is defined from the +current texts and `..._uncomparable_` from a side that could not be +serialised. Whether the engine evaluates `tendcf_cmdb` more than once is +measured by `tests/policy/run.sh` check 11 (debug-log counts on a real +cf-agent, with the file replaced between the evaluations); the check does not +depend on the answer. Reports print file-derived text only with characters +outside `[A-Za-z0-9._:@/-]` replaced by `?` and cut to a fixed length. +`tendcf-install.sh data` installs by atomic rename. Not checked: a file replaced +after the last evaluation (no check can see it). + +On any mismatch it defines the one class `tendcf_cmdb_refused`, and `tendcf_interlocks` reports what differs once. Both bundles then set `interlock_ids` and `service_ids` to explicitly -empty lists (an undefined list would leave a preset one in place), and the two +empty lists (an undefined list would leave a preset one in place; a literal +rvalue does redefine a preset, a function call does not), and the two `daemon-reload` commands, which iterate no id, carry the refusal themselves: no pre_action, no service, no tombstone. The vars and classes of a `bundle common` are pre-evaluated before the vars of any non-common bundle, whatever `bundlesequence` says (`PolicyResolve` and `BundleResolve`, `libpromises/expand.c` lines 846-871 and 1145-1177 at the pinned fork commit `b8abf28408b1`, [expand.c](https://github.com/djbclark/core/blob/b8abf28408b1b54e285d372a9bc5f2d850c960ad/libpromises/expand.c#L846)), -which is what makes the class final before any gate is read. That is a -guarantee about vars and classes only, not that every promise type of a common +so `tendcf_cmdb_refused` is decided before a gate is first read. That is a +statement about vars and classes only, not that every promise type of a common bundle completes first, and the classes inside the check rely on their written order within the one pass. `bundlesequence` guard classes in `promises.cf` are not needed for this check. This is a consistency check, not a sandbox: a CMDB diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index a7c3b0a..6028925 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -87,12 +87,25 @@ # same text, key order and number formatting included). A value that is not # an object is refused when it reads back differently: the loader installs # a scalar as a string (storejson() fails) and a list of primitives as an -# slist of strings (numbers read back as strings). Equal ids with changed -# values, as a `variables` entry or a -# replacement between the loader's read and ours would give, are caught; -# a comparison that cannot be computed counts as a difference; +# slist of strings (numbers read back as strings; a list of strings reads +# back equal and is accepted). Equal ids with changed values, as a +# `variables` entry would give, are caught. A comparison that cannot be +# computed is a problem of its own (`..._problem_uncomparable_`), not +# the absence of a difference; # - every `data:variables.*` the loader installed must be a key of `vars` # in the file; +# - nothing the loader installed may lie outside `data:variables.`: +# no CMDB-installed variable of any other name, and no CMDB-installed class. +# This is read from the loaded state (the variable table and the class +# table, by the tag the loader puts on everything it installs, +# `source=cmdb`), not from the file, in class expressions that call +# variablesmatching() and classmatch() inline, so it names no variable that +# a CMDB key could preset. It is what keeps the checker's own variables and +# the gates' id lists out of reach (see "Presets" below). It also covers the +# augments file (`def.json`, tag `source=augments_file`, loaded after the +# CMDB): tendcf ships none, and a variable or class from one is refused +# the same way (a host that needs a `def.json` must be given an exemption +# here, deliberately); # - a file that exists but cannot be parsed (or exceeds the loader's own # 5 MiB cap), and loaded data with no file at all, are mismatches. # A missing file with nothing loaded is the normal empty case: no class, no @@ -105,42 +118,71 @@ # On any mismatch the single class `tendcf_cmdb_refused` is defined and # NOTHING is actuated: no interlock pre_action, no service, no tombstone. The # gate is one place per bundle: `interlock_ids` and `service_ids` are set to -# empty lists under the refusal (explicitly, so a value a CMDB key preset in -# the bundle's scope is overwritten: variable redefinition is allowed unless a -# promise says `policy => "constant"`, verify_vars.c CollectConvergeVariable- -# Options), and every promise that acts iterates one of them. The two -# daemon-reload commands, which iterate no id, carry `!tendcf_cmdb_refused` -# themselves (a `classes` section could otherwise define their trigger -# classes; it is refused as well, but a refusal must not depend on that). A -# new actuating promise MUST iterate those ids or carry `!tendcf_cmdb_refused` -# itself. The mismatch is reported once, by tendcf_interlocks. +# empty lists under the refusal, and every promise that acts iterates one of +# them. The two daemon-reload commands, which iterate no id, carry +# `!tendcf_cmdb_refused` themselves (a `classes` section could otherwise define +# their trigger classes; it is refused as well, but a refusal must not depend on +# that). A new actuating promise MUST iterate those ids or carry +# `!tendcf_cmdb_refused` itself. The mismatch is reported once, by +# tendcf_interlocks. Reports print file-derived text (keys, entry ids, section +# names) only after replacing every character outside [A-Za-z0-9._:@/-] with `?` +# and cutting it to a fixed length; the refusal summary and the foreign +# variable and class reports are static text. # -# Order is load-bearing, as in the paragraph above. The fork evaluates the -# vars and the classes of every `bundle common` before the vars of any other -# bundle, independently of `bundlesequence`: PolicyResolve resolves the common -# bundles first and BundleResolve runs their vars, then classes, then vars -# again (libpromises/expand.c:846-871 and 1145-1177 at the commit pinned in -# overlay/fork.json, b8abf28408b1), so `tendcf_cmdb_refused` is final before -# the `!tendcf_cmdb_refused` on the agent bundles' `*_ids` is first read. That -# is a guarantee about vars and classes only, not that a common bundle's every -# promise type runs to completion first. Within the common bundle the classes +# Presets. The loader installs a `vars` key under any name, so a key such as +# `default:tendcf_cmdb.doc` or `default:tendcf_services.interlock_ids` presets +# that variable before any policy runs. Redefinition decides what a preset does, +# and it differs by the kind of rvalue (verify_vars.c VerifyVarPromise and +# CollectConvergeVariableOptions, commit b8abf28408b1 pinned in +# overlay/fork.json). A promise with a LITERAL rvalue (`slist => { }`, +# `string => "..."`) redefines an existing variable, so the preset is gone and +# the policy's value stands. A promise whose rvalue is a FUNCTION CALL +# (`readjson`, `getindices`, `difference`, `grep`, ... which is what every +# variable the checker computes and the non-refused id lists are) is skipped +# when the variable already exists ("Already did this"), so a preset keeps its +# value and the policy's own promise never runs. Presetting the checker's `doc`, +# `bad_keys` or `missing_keys`, or a gate's `interlock_ids`, would therefore +# change what the check compares or which interlocks gate a service. The fix is +# not to make each variable immune but to refuse the preset: a variable the +# CMDB installed under any name but `data:variables.` makes +# `tendcf_cmdb_problem_foreign_var`, read from the variable table with the +# `source=cmdb` tag, and a preset of a function-call variable keeps that tag. +# (A preset of a literal-rvalue variable is overwritten with the policy's value +# before it is read, and also refused when the tag is still there.) Under the +# refusal the id lists are literal empty lists, which do redefine a preset. +# +# Order and evaluation. The fork evaluates the vars and the classes of every +# `bundle common` before the vars of any other bundle, independently of +# `bundlesequence`: PolicyResolve resolves the common bundles first and +# BundleResolve runs their vars, then classes, then vars again +# (libpromises/expand.c:846-871 and 1145-1177 at the pinned commit), so +# `tendcf_cmdb_refused` is decided before the `!tendcf_cmdb_refused` on the agent +# bundles' `*_ids` is first read. That is a statement about vars and classes +# only, not that a common bundle's every promise type runs to completion first. +# Whether and how often the engine evaluates `tendcf_cmdb` again after that is +# measured, not assumed: tests/policy/run.sh check 11 counts the evaluations in +# the debug log of a real cf-agent and replaces the file between them. The +# check does not depend on the answer. Each problem class is defined from the +# CURRENT comparison, in positive form (a difference, an uncomputable side, a +# foreign variable), never as the negation of a class that was set once and +# stays set: a class is never undefined, and a promise for an already-set class +# is skipped (libpromises/promises.c), so "not equal" read from a sticky +# "equal" class would stay false after a later mismatch. A problem class, once +# set, stays set, which is the safe direction. The classes of the common bundle # run in one pass in written order: `tendcf_cmdb_refused` comes after every -# `tendcf_cmdb_problem_*` class, and `tendcf_cmdb_problem_content_*` reads the -# `tendcf_cmdb_json_equal_*` class defined just before it in the same section; -# do not reorder them. A class read before its definition is false and, once -# defined, never undone, so an agent bundle could not gate on a class it -# defines itself, and the check defines `tendcf_cmdb_problem_*` classes only -# from positive evidence (the content check reads its one negative form from -# the class defined immediately above it). Proved on a real cf-agent by -# tests/policy/run.sh checks 9-10 (a pre_action must not run, a service must -# not be processed). +# `tendcf_cmdb_problem_*` class; do not reorder them. # -# What this is NOT: a sandbox. It is a consistency check between the file and -# what the loader installed. A CMDB author (root) already chooses each -# pre_action executable, `/bin/sh -c ...` included, and the unit commands, so -# it does not stop a hostile but self-consistent file; it stops a file and a -# loader that disagree, and (above) the sections and keys through which a file -# could change what the policy reads without showing up as a `vars` difference. +# What is NOT guaranteed: the check compares the file with the loader's state at +# the evaluation. A file replaced after the last evaluation, or by a promise +# that changes `data:variables.*` afterwards, is not seen; and nothing here makes +# a publication atomic (`tendcf-install.sh data` installs by atomic rename, but +# the agent can still start between the loader's read and ours). Not a sandbox +# either: it is a consistency check between the file and what the loader +# installed. A CMDB author (root) already chooses each pre_action executable, +# `/bin/sh -c ...` included, and the unit commands, so it does not stop a hostile +# but self-consistent file; it stops a file and a loader that disagree, and the +# sections, keys, variables and classes through which a file could change what +# the policy reads without showing up as a `vars` difference. bundle common tendcf_cmdb { @@ -203,6 +245,40 @@ bundle common tendcf_cmdb "unlisted_keys" slist => difference("loaded_keys", "file_keys"); + # Printable forms for the reports: every character outside the safe set + # becomes `?`, then each name is cut to 64 characters (two steps, each the + # same maplist idiom as loaded_keys). Keys, ids and section names come + # from the file and may hold newlines or escape sequences. + "missing_san" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "missing_keys"); + "missing_clean" + slist => maplist(string_head("$(this)", 64), "missing_san"); + + "unlisted_san" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "unlisted_keys"); + "unlisted_clean" + slist => maplist(string_head("$(this)", 64), "unlisted_san"); + + "bad_san" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "bad_keys"); + "bad_clean" + slist => maplist(string_head("$(this)", 64), "bad_san"); + + "sections_san" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "extra_sections"); + "sections_clean" + slist => maplist(string_head("$(this)", 64), "sections_san"); + + "loaded_san" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "loaded_keys"); + "loaded_clean" + slist => maplist(string_head("$(this)", 64), "loaded_san"); + "key_canon[$(safe_keys)]" string => canonify("$(safe_keys)"); @@ -218,16 +294,36 @@ bundle common tendcf_cmdb "unfiled_ids[$(safe_keys)]" slist => difference("loaded_ids[$(safe_keys)]", "file_ids[$(safe_keys)]"); + "unloaded_san[$(safe_keys)]" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "unloaded_ids[$(safe_keys)]"); + + "unloaded_clean[$(safe_keys)]" + slist => maplist(string_head("$(this)", 64), "unloaded_san[$(safe_keys)]"); + + "unloaded_joined[$(safe_keys)]" + string => join(", ", "unloaded_clean[$(safe_keys)]"); + "unloaded_text[$(safe_keys)]" - string => join(", ", "unloaded_ids[$(safe_keys)]"); + string => string_head("$(unloaded_joined[$(safe_keys)])", 400); + + "unfiled_san[$(safe_keys)]" + slist => maplist(regex_replace("$(this)", "[^A-Za-z0-9._:@/-]", "?", "g"), + "unfiled_ids[$(safe_keys)]"); + + "unfiled_clean[$(safe_keys)]" + slist => maplist(string_head("$(this)", 64), "unfiled_san[$(safe_keys)]"); + + "unfiled_joined[$(safe_keys)]" + string => join(", ", "unfiled_clean[$(safe_keys)]"); "unfiled_text[$(safe_keys)]" - string => join(", ", "unfiled_ids[$(safe_keys)]"); + string => string_head("$(unfiled_joined[$(safe_keys)])", 400); # The whole container, as JSON text, from the file and as installed. # storejson() fails (the variable stays undefined) for anything that is - # not a container or list, and for a name that is not defined; the content - # class below treats an undefined side as a difference. + # not a container or list, and for a name that is not defined; the + # `uncomparable` class below makes an undefined side a problem. "file_json[$(safe_keys)]" string => storejson("doc[vars][$(safe_keys)]"); @@ -255,23 +351,40 @@ bundle common tendcf_cmdb "tendcf_cmdb_problem_badkey" expression => isgreaterthan(length("bad_keys"), 0); + # Read from the loaded state, with the calls inline: no variable here for + # a CMDB key to preset. Every variable tagged by the CMDB loader or by the + # augments loader is counted, and so is every one of them that is exactly + # data:variables. AND tagged by the CMDB loader; any excess is + # a variable of another name (or from augments), which is a problem. + "tendcf_cmdb_problem_foreign_var" + expression => isgreaterthan(length(variablesmatching(".*", "source=cmdb", "source=augments_file")), + length(variablesmatching("data:variables\.[A-Za-z0-9_]+", "source=cmdb"))); + + "tendcf_cmdb_problem_foreign_class" + expression => classmatch(".*", "source=cmdb", "source=augments_file"); + "tendcf_cmdb_problem_partial_$(key_canon[$(safe_keys)])" expression => and(isvariable("data:variables.$(safe_keys)"), or(isgreaterthan(length("unloaded_ids[$(safe_keys)]"), 0), isgreaterthan(length("unfiled_ids[$(safe_keys)]"), 0))); - # Equal containers: defined only when both texts exist and are identical. - # The next promise reads this class negatively, which is sound only - # because it is evaluated after this one in the same pass (see the - # header): a loaded container that is not provably equal is a problem, - # whether the texts differ or one of them could not be computed. - "tendcf_cmdb_json_equal_$(key_canon[$(safe_keys)])" - expression => strcmp("$(file_json[$(safe_keys)])", - "$(loaded_json[$(safe_keys)])"); - - "tendcf_cmdb_problem_content_$(key_canon[$(safe_keys)])" + # The two texts, computed now, differ. Defined only when both exist (an + # undefined side leaves the strcmp() argument unexpanded and the + # expression undefined; that case is the next class). + "tendcf_cmdb_problem_differs_$(key_canon[$(safe_keys)])" + expression => and(isvariable("data:variables.$(safe_keys)"), + isvariable("file_json[$(safe_keys)]"), + isvariable("loaded_json[$(safe_keys)]"), + not(strcmp("$(file_json[$(safe_keys)])", + "$(loaded_json[$(safe_keys)])"))); + + # A loaded container for which one side could not be serialised. Needs only + # isvariable(), whose arguments are literal names, so it cannot be + # suppressed by an undefined variable. + "tendcf_cmdb_problem_uncomparable_$(key_canon[$(safe_keys)])" expression => and(isvariable("data:variables.$(safe_keys)"), - not("tendcf_cmdb_json_equal_$(key_canon[$(safe_keys)])")); + or(not(isvariable("file_json[$(safe_keys)]")), + not(isvariable("loaded_json[$(safe_keys)]")))); "tendcf_cmdb_refused" expression => classmatch("tendcf_cmdb_problem_.*"); @@ -376,25 +489,36 @@ bundle agent tendcf_interlocks "tendcf: CMDB cross-check: $(tendcf_cmdb.file) exists but cannot be read as JSON (parse error or larger than 5242880 bytes)"; "tendcf_cmdb_problem_nofile":: - "tendcf: CMDB cross-check: $(tendcf_cmdb.file) does not exist but CFEngine loaded data:variables.$(tendcf_cmdb.loaded_keys)"; + "tendcf: CMDB cross-check: $(tendcf_cmdb.file) does not exist but CFEngine loaded data:variables.$(tendcf_cmdb.loaded_clean)"; "tendcf_cmdb_problem_missing":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.missing_keys)' is under 'vars' in $(tendcf_cmdb.file) but CFEngine did not load data:variables.$(tendcf_cmdb.missing_keys) (the loader skipped it)"; + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.missing_clean)' is under 'vars' in $(tendcf_cmdb.file) but CFEngine did not load data:variables.$(tendcf_cmdb.missing_clean) (the loader skipped it)"; "tendcf_cmdb_problem_unlisted":: - "tendcf: CMDB cross-check: CFEngine loaded data:variables.$(tendcf_cmdb.unlisted_keys), which is not a key under 'vars' in $(tendcf_cmdb.file)"; + "tendcf: CMDB cross-check: CFEngine loaded data:variables.$(tendcf_cmdb.unlisted_clean), which is not a key under 'vars' in $(tendcf_cmdb.file)"; "tendcf_cmdb_problem_partial_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.safe_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.safe_keys)])]"; - "tendcf_cmdb_problem_content_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content differs between $(tendcf_cmdb.file) and the data CFEngine loaded (or the two could not be compared)"; + "tendcf_cmdb_problem_differs_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content differs between $(tendcf_cmdb.file) and the data CFEngine loaded"; + + "tendcf_cmdb_problem_uncomparable_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content could not be compared between $(tendcf_cmdb.file) and the data CFEngine loaded (one side is not a container or list)"; "tendcf_cmdb_problem_sections":: - "tendcf: CMDB cross-check: $(tendcf_cmdb.file) has top-level section '$(tendcf_cmdb.extra_sections)' besides 'vars'; only 'vars' is accepted (the loader would also apply 'variables' and 'classes')"; + "tendcf: CMDB cross-check: $(tendcf_cmdb.file) has top-level section '$(tendcf_cmdb.sections_clean)' besides 'vars'; only 'vars' is accepted (the loader would also apply 'variables' and 'classes')"; + + # Static text on purpose: the names are in `cf-promises --show-vars` + # (tag source=cmdb or source=augments_file) and `--show-classes`. + "tendcf_cmdb_problem_foreign_var":: + "tendcf: CMDB cross-check: the CMDB or an augments file installed a variable outside data:variables. (a preset of a variable of this policy or of another bundle); only data:variables. is accepted"; + + "tendcf_cmdb_problem_foreign_class":: + "tendcf: CMDB cross-check: the CMDB or an augments file defined a class; no class from either is accepted"; "tendcf_cmdb_problem_badkey":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.bad_keys)' under 'vars' in $(tendcf_cmdb.file) is not a plain identifier ([A-Za-z0-9_]+); the loader would read it as a variable reference"; + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.bad_clean)' under 'vars' in $(tendcf_cmdb.file) is not a plain identifier ([A-Za-z0-9_]+); the loader would read it as a variable reference"; } bundle agent tendcf_services diff --git a/tests/policy/run.sh b/tests/policy/run.sh index 9ebbd90..c1b188c 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -62,8 +62,15 @@ # `classes` section, and a `vars` key that is not a plain identifier, which # the loader would file under another scope (a preset of the policy's own # id lists), and a `vars` value that is not an object (not comparable as -# JSON text with the file's). Every file of checks 2-8 and 6 is also a -# no-report control. +# JSON text with the file's). Also refused, each actuating nothing: a CMDB +# `vars` key that presets a variable of the checker itself (`doc`, +# `bad_keys`, `missing_keys`) or of a gate (`interlock_ids`), which the +# engine would otherwise keep because a vars promise with a function-call +# rvalue is skipped when the variable exists; a `classes` section that is +# hidden from the file-based checks by such a preset; and an augments file +# (`def.json` in inputs/) that defines a class and a variable. A `vars` +# container that is a list of strings is the positive control: accepted, +# actuates. Every file of checks 2-8 and 6 is also a no-report control. # NB: the fork's loader skips a bad entry and installs the rest, but the # Mac dev build rejects the whole `vars` instead (the same fixture refuses # on both, as "nothing loaded"), so only the fork's CI legs see the @@ -81,6 +88,13 @@ # interlock's pre_action) are refused as a content difference, which no # key-and-id comparison could see. Under TENDCF_TEST_SYSTEMD=1 the same stand-in is run against # the real manager (6b): no unit rendered, a tombstone leaves the unit alone. +# 11. the engine's evaluation of tendcf_cmdb, measured on a real +# cf-agent with -d: how often the checker's variables and classes are +# visited, with the file replaced (same keys and ids, another pre_action) +# by a common bundle resolved after tendcf_cmdb. The run must never act on +# the replaced file: either it is refused and nothing actuates, or the +# loaded pre_action runs and the replaced one does not. The counts are +# printed (`info:`) so a change in the engine's evaluation order is visible. # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist # on Linux and macOS alike; /bin/true does not exist on macOS, and a missing @@ -618,18 +632,18 @@ check "scoped-key preset (control): the loader installed default:tendcf_services # A `vars` value that is not an object is installed as a scalar (not # comparable: storejson() fails) or, for a list of primitives, as an slist of # strings, so numbers read back as "1","2" and differ from the file's [1,2]: -# either way a difference, refused and named. (A list of strings would compare -# equal and pass, which is correct.) +# either way a problem (a difference, or one side that cannot be serialised), +# refused and named. (A list of strings compares equal and passes, below.) printf '%s\n' '{"vars":{"tendcf_interlock":{},"tendcf_service":{},"scalar":"x","plain_list":[1,2]}}' >"$work/data/host_specific.json" out="$work/xc-uncomparable.out" "$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true check "uncomparable vars values: refusal reported" refusal_reported "$out" for k in scalar plain_list; do - check "uncomparable vars value '$k': named as a content difference" \ - grep -q "key '$k' content differs between" "$out" + check "uncomparable vars value '$k': named (differs or could not be compared)" \ + grep -Eq "key '$k' content (differs|could not be compared) between" "$out" done check "uncomparable vars values: the object containers are not blamed" \ - bash -c "! grep -Eq \"key 'tendcf_(interlock|service)' content differs\" '$out'" + bash -c "! grep -Eq \"key 'tendcf_(interlock|service)' content (differs|could not be compared)\" '$out'" # A `vars` key can preset any variable of the policy, the checker's own included # (adversary M1): a vars promise whose rvalue is a function call is skipped when From 1d5b32162864e47417cf5c4dd77bb904965848a3 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 13:12:50 -0500 Subject: [PATCH 07/13] policy: read the file once (doc snapshot) so no later pass compares or acts on a replaced file; qualify the preset and evaluation docs to what CI measured; classes-hidden asserts the class and section Check 11 on d288c81 showed tendcf_cmdb's vars, readjson of doc included, are visited on every pass while its classes are decided once, so a later pass read a replaced file the decision never saw. doc is now skipped once it exists. Co-Authored-By: Claude Sonnet 5.5 --- policy/tendcf_services.cf | 82 ++++++++++++++++++++------------------- tests/policy/run.sh | 19 +++++++-- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index 6028925..d4c96d9 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -131,46 +131,45 @@ # # Presets. The loader installs a `vars` key under any name, so a key such as # `default:tendcf_cmdb.doc` or `default:tendcf_services.interlock_ids` presets -# that variable before any policy runs. Redefinition decides what a preset does, -# and it differs by the kind of rvalue (verify_vars.c VerifyVarPromise and -# CollectConvergeVariableOptions, commit b8abf28408b1 pinned in -# overlay/fork.json). A promise with a LITERAL rvalue (`slist => { }`, -# `string => "..."`) redefines an existing variable, so the preset is gone and -# the policy's value stands. A promise whose rvalue is a FUNCTION CALL -# (`readjson`, `getindices`, `difference`, `grep`, ... which is what every -# variable the checker computes and the non-refused id lists are) is skipped -# when the variable already exists ("Already did this"), so a preset keeps its -# value and the policy's own promise never runs. Presetting the checker's `doc`, -# `bad_keys` or `missing_keys`, or a gate's `interlock_ids`, would therefore -# change what the check compares or which interlocks gate a service. The fix is -# not to make each variable immune but to refuse the preset: a variable the -# CMDB installed under any name but `data:variables.` makes -# `tendcf_cmdb_problem_foreign_var`, read from the variable table with the -# `source=cmdb` tag, and a preset of a function-call variable keeps that tag. -# (A preset of a literal-rvalue variable is overwritten with the policy's value -# before it is read, and also refused when the tag is still there.) Under the -# refusal the id lists are literal empty lists, which do redefine a preset. +# that variable before any policy runs. What a preset does then depends on the +# promise (verify_vars.c VerifyVarPromise, commit b8abf28408b1 pinned in +# overlay/fork.json): a promise with a LITERAL rvalue (`slist => { }`) always +# redefines it, so the policy's value stands. A promise whose rvalue is a +# FUNCTION CALL is skipped when the variable exists ("Already did this") if the +# call was not already evaluated to a value when the promise runs, and +# otherwise redefines it; which of the two happens is not something this policy +# relies on (tests/policy/run.sh check 9 saw both: some presets of the checker's +# and the gates' variables stayed, some were replaced by the policy's value). +# So the check does not try to make each variable immune: any variable the CMDB +# (or an augments file) installed under a name other than +# `data:variables.` and still carrying the loader's tag when the +# classes are evaluated makes `tendcf_cmdb_problem_foreign_var`, read from the +# variable table, and a preset that was replaced is the policy's own value. The +# checker's `doc` is guarded to be read once, so a preset of it is never +# overwritten by a later pass and is refused by that class. Under the refusal +# the id lists are literal empty lists, which redefine a preset. # -# Order and evaluation. The fork evaluates the vars and the classes of every +# Order and evaluation, as measured on the pinned fork (tests/policy/run.sh +# check 11, debug log of a real cf-agent): `tendcf_cmdb` is resolved twice, its +# vars are visited on every pass, the readjson of `doc` included, and its +# classes are decided once. The fork evaluates the vars and the classes of every # `bundle common` before the vars of any other bundle, independently of -# `bundlesequence`: PolicyResolve resolves the common bundles first and -# BundleResolve runs their vars, then classes, then vars again -# (libpromises/expand.c:846-871 and 1145-1177 at the pinned commit), so -# `tendcf_cmdb_refused` is decided before the `!tendcf_cmdb_refused` on the agent -# bundles' `*_ids` is first read. That is a statement about vars and classes -# only, not that a common bundle's every promise type runs to completion first. -# Whether and how often the engine evaluates `tendcf_cmdb` again after that is -# measured, not assumed: tests/policy/run.sh check 11 counts the evaluations in -# the debug log of a real cf-agent and replaces the file between them. The -# check does not depend on the answer. Each problem class is defined from the -# CURRENT comparison, in positive form (a difference, an uncomputable side, a -# foreign variable), never as the negation of a class that was set once and -# stays set: a class is never undefined, and a promise for an already-set class -# is skipped (libpromises/promises.c), so "not equal" read from a sticky -# "equal" class would stay false after a later mismatch. A problem class, once -# set, stays set, which is the safe direction. The classes of the common bundle -# run in one pass in written order: `tendcf_cmdb_refused` comes after every -# `tendcf_cmdb_problem_*` class; do not reorder them. +# `bundlesequence` (PolicyResolve and BundleResolve, libpromises/expand.c:846-871 +# and 1145-1177), so `tendcf_cmdb_refused` is decided, on the first pass over +# the file, before the `!tendcf_cmdb_refused` on the agent bundles' `*_ids` is +# first read. A later vars pass that re-read the file would see a replacement +# the decision never compared, so `doc` is read ONCE: its promise is skipped +# once `doc` exists, every comparison input is derived from that one snapshot, +# and no later pass reads the file. The probe of check 11 replaces the file +# between the passes and shows that the loaded data are acted on and the +# replacement is not. Each problem class is defined from the current comparison, +# in positive form (a difference, an uncomputable side, a foreign variable), +# never as the negation of a class set once and never cleared: a promise for an +# already-set class is skipped (libpromises/promises.c), so "not equal" read +# from a sticky "equal" class would stay false after a later mismatch. The +# classes of the common bundle run in one pass in written order: +# `tendcf_cmdb_refused` comes after every `tendcf_cmdb_problem_*` class; do not +# reorder them. # # What is NOT guaranteed: the check compares the file with the loader's state at # the evaluation. A file replaced after the last evaluation, or by a promise @@ -192,9 +191,14 @@ bundle common tendcf_cmdb # 5242880 = the loader's HOST_SPECIFIC_DATA_MAX_SIZE (5 MiB). An absent # file leaves `doc` undefined and is not an error. + # + # Read ONCE (see "Order and evaluation"): the engine visits this promise on + # every later pass, and a second read could return a file the classes never + # compared. `not(isvariable("doc"))` skips it once `doc` exists, a preset + # included (which is then refused as a foreign variable). "doc" data => readjson("$(file)", 5242880), - ifvarclass => fileexists("$(file)"); + ifvarclass => and(fileexists("$(file)"), not(isvariable("doc"))); "file_keys" slist => getindices("doc[vars]"), diff --git a/tests/policy/run.sh b/tests/policy/run.sh index c1b188c..a921d30 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -657,11 +657,18 @@ for fx in checker-preset checker-preset-keys classes-hidden; do out="$work/xc-$fx.out" "$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true check "$fx: refusal reported" refusal_reported "$out" - check "$fx: the foreign variable is reported" foreign_var_reported "$out" + if [ "$fx" != classes-hidden ]; then + check "$fx: the foreign variable is reported" foreign_var_reported "$out" + fi check "$fx: refusal reported once" refusal_once "$out" check "$fx: nothing actuated (no pre_action, no service processed)" no_actuation "$out" done +# Its presets of the checker's variables are replaced by the policy's own values +# (check 11 shows function-call vars are re-evaluated), so the section and the +# class are what the policy sees; the foreign class is the loaded-state evidence. check "classes-hidden: the foreign class is reported" foreign_class_reported "$work/xc-classes-hidden.out" +check "classes-hidden: the section is reported too" \ + grep -q "has top-level section 'classes' besides 'vars'" "$work/xc-classes-hidden.out" # Augments (def.json in inputs/) are loaded after the CMDB and can define classes # and variables just the same. tendcf ships none, so any is refused. @@ -765,9 +772,13 @@ echo "# 11. when does the engine evaluate tendcf_cmdb? (tests/policy/probe-mutat # The file is consistent with what the loader installed (A). A common bundle # resolved after tendcf_cmdb replaces it with B: same keys and ids, another # pre_action command. If the engine evaluated tendcf_cmdb's variables or classes -# again, the policy would read B; it must then refuse everything. If it does not, -# the pre_action of A runs, never B's. Either way B is never acted on. The debug -# log (-d) counts the evaluations, so a later engine change shows up here. +# again and read the file, the policy would see B; it must then refuse +# everything. The policy reads the file once (the `doc` promise is skipped once +# `doc` exists), so the expected outcome is the second one: A was compared and +# the loaded pre_action (A's) runs, never B's. Either way B is never acted on. +# Measured on the pinned fork (2026-11-14, Linux CI): tendcf_cmdb is resolved +# twice and its vars are visited on every pass, its classes once. The debug log +# (-d) counts the visits, so a later engine change shows up as a changed count. cat > "$work/inputs/probe-entry.cf" < Date: Sat, 14 Nov 2026 13:19:55 -0500 Subject: [PATCH 08/13] tests: probe measures the outcome on a run without -d (a debug log prints the policy's own report text, which the refusal grep matched), pins the single expected outcome and the one read of the file Co-Authored-By: Claude Sonnet 5.5 --- tests/policy/run.sh | 55 ++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/tests/policy/run.sh b/tests/policy/run.sh index a921d30..69edd35 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -91,10 +91,10 @@ # 11. the engine's evaluation of tendcf_cmdb, measured on a real # cf-agent with -d: how often the checker's variables and classes are # visited, with the file replaced (same keys and ids, another pre_action) -# by a common bundle resolved after tendcf_cmdb. The run must never act on -# the replaced file: either it is refused and nothing actuates, or the -# loaded pre_action runs and the replaced one does not. The counts are -# printed (`info:`) so a change in the engine's evaluation order is visible. +# by a common bundle resolved after tendcf_cmdb. The policy reads the file +# once, so the run is not refused, the loaded pre_action runs and the +# replaced one does not; a second run with the debug log counts the reads +# (the doc promise is evaluated once) and prints the other counts (`info:`). # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist # on Linux and macOS alike; /bin/true does not exist on macOS, and a missing @@ -772,10 +772,10 @@ echo "# 11. when does the engine evaluate tendcf_cmdb? (tests/policy/probe-mutat # The file is consistent with what the loader installed (A). A common bundle # resolved after tendcf_cmdb replaces it with B: same keys and ids, another # pre_action command. If the engine evaluated tendcf_cmdb's variables or classes -# again and read the file, the policy would see B; it must then refuse -# everything. The policy reads the file once (the `doc` promise is skipped once -# `doc` exists), so the expected outcome is the second one: A was compared and -# the loaded pre_action (A's) runs, never B's. Either way B is never acted on. +# again and read the file, the policy would see B and, were the comparison +# repeated, refuse. The policy reads the file once (the `doc` promise is skipped +# once `doc` exists), so the one expected outcome is: A was compared, the loaded +# pre_action (A's) runs, B's never does. # Measured on the pinned fork (2026-11-14, Linux CI): tendcf_cmdb is resolved # twice and its vars are visited on every pass, its classes once. The debug log # (-d) counts the visits, so a later engine change shows up as a changed count. @@ -796,27 +796,36 @@ cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_speci probe_a="$(cat "$repo/tests/policy/fixtures/interlock-satisfied.json")" printf '%s\n' "${probe_a//\"\/usr\/bin\/true\"/\"\/usr\/bin\/false\"}" > "$work/probe-B.json" rm -f "$work/probe-count" +# Outcome: a run without -d, so that the log holds only what the agent did. (A +# debug log (-d) prints the policy's own report strings, so grepping it for a +# refusal line finds the policy text, not a refusal: the first version of this +# check read "refused" from exactly that.) out="$work/xc-probe.out" -"$cf_agent" -K -I -d -f "$work/inputs/probe-entry.cf" >"$out" 2>&1 || true -n_doc="$(grep -c 'Evaluating vars promise: doc$' "$out" || true)" -n_json="$(grep -c 'Evaluating vars promise: file_json\[' "$out" || true)" -n_cls="$(grep -c 'Evaluating classes promise: tendcf_cmdb_refused$' "$out" || true)" -n_res="$(grep -c "Resolving classes and variables in 'bundle common tendcf_cmdb'" "$out" || true)" +"$cf_agent" -K -I -f "$work/inputs/probe-entry.cf" >"$out" 2>&1 || true +check "probe: the file was replaced during the run" \ + bash -c "[ \"\$(cat '$work/data/host_specific.json')\" = \"\$(cat '$work/probe-B.json')\" ]" +check "probe: not refused: the policy never saw the replacement (it reads the file once)" \ + no_crosscheck "$out" +check "probe: the loaded pre_action (true) ran" grep -q "info: Executing .*'/usr/bin/true'" "$out" +check "probe: the replaced pre_action (false) did not" \ + bash -c "! grep -q \"info: Executing .*'/usr/bin/false'\" '$out'" +# Counts: a second run with the debug log, from the same starting file. +cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_specific.json" +rm -f "$work/probe-count" +dbg="$work/xc-probe-debug.out" +"$cf_agent" -K -I -d -f "$work/inputs/probe-entry.cf" >"$dbg" 2>&1 || true +n_doc="$(grep -c 'Evaluating vars promise: doc$' "$dbg" || true)" +n_json="$(grep -c 'Evaluating vars promise: file_json\[' "$dbg" || true)" +n_cls="$(grep -c 'Evaluating classes promise: tendcf_cmdb_refused$' "$dbg" || true)" +n_res="$(grep -c "Resolving classes and variables in 'bundle common tendcf_cmdb'" "$dbg" || true)" n_mut="$(wc -l < "$work/probe-count" 2>/dev/null || echo 0)" echo "info: tendcf_cmdb resolved $n_res times; vars 'doc' visited $n_doc times, 'file_json[..]' $n_json times; class 'tendcf_cmdb_refused' visited $n_cls times; mutation script ran $n_mut times" -check "probe: the mutation script ran" [ "$n_mut" -ge 1 ] -if refusal_reported "$out"; then - echo "info: the engine evaluated tendcf_cmdb again and the policy read the replaced file" - check "probe: refused after the replacement, nothing actuated" no_actuation "$out" -else - echo "info: the policy did not read the replaced file (tendcf_cmdb is not evaluated again)" - check "probe: not refused, the loaded pre_action (true) ran and the replaced one (false) did not" \ - bash -c "grep -q \"Executing .*'/usr/bin/true'\" '$out' && ! grep -q \"Executing .*'/usr/bin/false'\" '$out'" -fi +check "probe (debug run): the mutation script ran" [ "$n_mut" -ge 1 ] +check "probe (debug run): the file is read once (doc promise evaluated once)" [ "$n_doc" = 1 ] if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist xc-probe \ xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue From 87a4b585cc180b5004011fa751b1f808b1719a2d Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 13:27:05 -0500 Subject: [PATCH 09/13] tests: make the replaced-file probe deterministic: before the check's read (refused, content differs) and after it (not refused, loaded pre_action runs), each with one outcome Co-Authored-By: Claude Sonnet 5.5 --- tests/policy/probe-mutate.cf | 25 +++++++---- tests/policy/run.sh | 87 +++++++++++++++++++++--------------- 2 files changed, 69 insertions(+), 43 deletions(-) diff --git a/tests/policy/probe-mutate.cf b/tests/policy/probe-mutate.cf index 917202f..333c73b 100644 --- a/tests/policy/probe-mutate.cf +++ b/tests/policy/probe-mutate.cf @@ -1,14 +1,23 @@ -# Test only (tests/policy/run.sh check 11). A `bundle common` listed AFTER -# policy/tendcf_services.cf in `inputs`, so PolicyResolve resolves it after -# tendcf_cmdb has finished its first vars, classes and vars passes. Its one vars -# promise runs a script that replaces host_specific.json with a file that has the -# same keys and entry ids but another pre_action command, i.e. a publication -# between two evaluations of tendcf_cmdb. What the policy does after that shows -# whether, and when, the engine evaluates tendcf_cmdb again. +# Test only (tests/policy/run.sh check 11). A `bundle common` whose one job is to +# replace host_specific.json with a file that has the same keys and entry ids but +# another pre_action command, i.e. a publication during the run. Two timings, by +# class (cf-agent -D), so each is deterministic rather than a matter of +# evaluation order: +# probe_before the replacement is made from the first evaluation of this +# bundle; the entry lists this file BEFORE tendcf_services.cf, +# so it runs before tendcf_cmdb reads the file. +# probe_after the replacement is made only once tendcf_cmdb.doc exists, i.e. +# after tendcf_cmdb's read of the file, whatever the order. bundle common tendcf_probe_mutate { vars: - "ran" + probe_before:: + "ran_before" string => execresult("$(sys.workdir)/probe-mutate.sh", "noshell"); + + probe_after:: + "ran_after" + string => execresult("$(sys.workdir)/probe-mutate.sh", "noshell"), + ifvarclass => isvariable("tendcf_cmdb.doc"); } diff --git a/tests/policy/run.sh b/tests/policy/run.sh index 69edd35..c09ecf8 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -91,10 +91,11 @@ # 11. the engine's evaluation of tendcf_cmdb, measured on a real # cf-agent with -d: how often the checker's variables and classes are # visited, with the file replaced (same keys and ids, another pre_action) -# by a common bundle resolved after tendcf_cmdb. The policy reads the file -# once, so the run is not refused, the loaded pre_action runs and the -# replaced one does not; a second run with the debug log counts the reads -# (the doc promise is evaluated once) and prints the other counts (`info:`). +# by a common bundle, in two deterministic timings: before the check's read +# (refused, "content differs", nothing runs) and after it (the policy reads +# the file once, so not refused, the loaded pre_action runs and the replaced +# one does not); a debug run counts the reads (the doc promise is evaluated +# once) and prints the other counts (`info:`). # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist # on Linux and macOS alike; /bin/true does not exist on macOS, and a missing @@ -768,18 +769,28 @@ check "data loaded with no file: reported" \ grep -q "host_specific.json does not exist but CFEngine loaded data:variables.stray" "$out" check "data loaded with no file: nothing actuated" no_actuation "$out" -echo "# 11. when does the engine evaluate tendcf_cmdb? (tests/policy/probe-mutate.cf)" -# The file is consistent with what the loader installed (A). A common bundle -# resolved after tendcf_cmdb replaces it with B: same keys and ids, another -# pre_action command. If the engine evaluated tendcf_cmdb's variables or classes -# again and read the file, the policy would see B and, were the comparison -# repeated, refuse. The policy reads the file once (the `doc` promise is skipped -# once `doc` exists), so the one expected outcome is: A was compared, the loaded -# pre_action (A's) runs, B's never does. -# Measured on the pinned fork (2026-11-14, Linux CI): tendcf_cmdb is resolved -# twice and its vars are visited on every pass, its classes once. The debug log -# (-d) counts the visits, so a later engine change shows up as a changed count. -cat > "$work/inputs/probe-entry.cf" < "$work/inputs/probe-before.cf" < { "$repo/tests/policy/probe-mutate.cf", "$repo/policy/tendcf_services.cf" }; + bundlesequence => { "tendcf_interlocks", "tendcf_services" }; +} +EOF2 +cat > "$work/inputs/probe-after.cf" < { "$repo/policy/tendcf_services.cf", "$repo/tests/policy/probe-mutate.cf" }; @@ -792,28 +803,34 @@ echo ran >> "$work/probe-count" cp "$work/probe-B.json" "$work/data/host_specific.json" EOF2 chmod 700 "$work/probe-mutate.sh" -cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_specific.json" probe_a="$(cat "$repo/tests/policy/fixtures/interlock-satisfied.json")" printf '%s\n' "${probe_a//\"\/usr\/bin\/true\"/\"\/usr\/bin\/false\"}" > "$work/probe-B.json" -rm -f "$work/probe-count" -# Outcome: a run without -d, so that the log holds only what the agent did. (A -# debug log (-d) prints the policy's own report strings, so grepping it for a -# refusal line finds the policy text, not a refusal: the first version of this -# check read "refused" from exactly that.) -out="$work/xc-probe.out" -"$cf_agent" -K -I -f "$work/inputs/probe-entry.cf" >"$out" 2>&1 || true -check "probe: the file was replaced during the run" \ - bash -c "[ \"\$(cat '$work/data/host_specific.json')\" = \"\$(cat '$work/probe-B.json')\" ]" -check "probe: not refused: the policy never saw the replacement (it reads the file once)" \ - no_crosscheck "$out" -check "probe: the loaded pre_action (true) ran" grep -q "info: Executing .*'/usr/bin/true'" "$out" -check "probe: the replaced pre_action (false) did not" \ +probe_replaced() { [ "$(cat "$work/data/host_specific.json")" = "$(cat "$work/probe-B.json")" ]; } +probe_start() { cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_specific.json"; rm -f "$work/probe-count"; } + +probe_start +out="$work/xc-probe-before.out" +"$cf_agent" -K -I -D probe_before -f "$work/inputs/probe-before.cf" >"$out" 2>&1 || true +check "probe before the read: the file was replaced" probe_replaced +check "probe before the read: refused" refusal_reported "$out" +check "probe before the read: the content difference is named" \ + grep -q "key 'tendcf_interlock' content differs between" "$out" +check "probe before the read: nothing ran (neither the loaded nor the replaced pre_action)" no_actuation "$out" + +probe_start +out="$work/xc-probe-after.out" +"$cf_agent" -K -I -D probe_after -f "$work/inputs/probe-after.cf" >"$out" 2>&1 || true +check "probe after the read: the file was replaced" probe_replaced +check "probe after the read: not refused (the policy never saw the replacement)" no_crosscheck "$out" +check "probe after the read: the loaded pre_action (true) ran" \ + grep -q "info: Executing .*'/usr/bin/true'" "$out" +check "probe after the read: the replaced pre_action (false) did not" \ bash -c "! grep -q \"info: Executing .*'/usr/bin/false'\" '$out'" -# Counts: a second run with the debug log, from the same starting file. -cp "$repo/tests/policy/fixtures/interlock-satisfied.json" "$work/data/host_specific.json" -rm -f "$work/probe-count" + +# Visit counts, from a debug run of the after case. +probe_start dbg="$work/xc-probe-debug.out" -"$cf_agent" -K -I -d -f "$work/inputs/probe-entry.cf" >"$dbg" 2>&1 || true +"$cf_agent" -K -I -d -D probe_after -f "$work/inputs/probe-after.cf" >"$dbg" 2>&1 || true n_doc="$(grep -c 'Evaluating vars promise: doc$' "$dbg" || true)" n_json="$(grep -c 'Evaluating vars promise: file_json\[' "$dbg" || true)" n_cls="$(grep -c 'Evaluating classes promise: tendcf_cmdb_refused$' "$dbg" || true)" @@ -825,7 +842,7 @@ check "probe (debug run): the file is read once (doc promise evaluated once)" [ if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist xc-probe \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist xc-probe-before xc-probe-after \ xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue From db1e73e7b1f332e1b046245ab7544dd5e43b42c0 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 13:37:45 -0500 Subject: [PATCH 10/13] docs: proposal paragraph states the single read of doc, the measured evaluation counts and the two probe timings; presets may survive or be replaced Co-Authored-By: Claude Sonnet 5.5 --- .../tendcf-validate-proposal-2026-11-03.md | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index 730a167..da40776 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1153,31 +1153,34 @@ that is not a `vars` key of the file, a file that exists but cannot be parsed, and loaded data with no file. A missing file with nothing loaded is the normal empty case and says nothing. -Round 2 of the review (Astra A1, adversary M1) added the loaded-state check. -A preset reaches the checker itself: a vars promise whose rvalue is a function -call (`readjson`, `getindices`, `difference`, every variable the checker -computes and the non-refused id lists) is skipped when the variable already -exists (`verify_vars.c`, "Already did this"; only a promise with a literal -rvalue redefines it). So `default:tendcf_cmdb.doc`, -`default:tendcf_cmdb.bad_keys` or `default:tendcf_services.interlock_ids` in -the file keeps its value, and the check passes while the loaded interlock is -ignored. The policy therefore reads the loaded state directly, in class -expressions that call the functions inline (no variable to preset): any -variable tagged `source=cmdb` or `source=augments_file` outside +Round 2 of the review (Astra A1, adversary M1) added the loaded-state check +and the single read. A preset reaches the checker itself: a `vars` key such as +`default:tendcf_cmdb.doc` or `default:tendcf_services.interlock_ids` is +installed before the policy runs. Whether it survives depends on the promise: a +promise whose rvalue is a function call is skipped when the variable exists +(`verify_vars.c`, "Already did this") if the call was not already evaluated, +and otherwise redefines it; the CI runs of check 9 saw presets of both kinds. +The policy does not rely on which: it reads the loaded state directly, in class +expressions that call the functions inline (no variable to preset), and refuses +any variable tagged `source=cmdb` or `source=augments_file` outside `data:variables.` (`variablesmatching`) and any class with either -tag (`classmatch`) is refused. tendcf ships no `def.json`, so augments are -refused too (a host that needs one must be exempted in the policy, on -purpose). The content comparison no longer reads the negation of a class set -once and never cleared (`tendcf_cmdb_json_equal_*`, which a later mismatch -could not unset); `tendcf_cmdb_problem_differs_` is defined from the -current texts and `..._uncomparable_` from a side that could not be -serialised. Whether the engine evaluates `tendcf_cmdb` more than once is -measured by `tests/policy/run.sh` check 11 (debug-log counts on a real -cf-agent, with the file replaced between the evaluations); the check does not -depend on the answer. Reports print file-derived text only with characters -outside `[A-Za-z0-9._:@/-]` replaced by `?` and cut to a fixed length. -`tendcf-install.sh data` installs by atomic rename. Not checked: a file replaced -after the last evaluation (no check can see it). +tag (`classmatch`). tendcf ships no `def.json`, so augments are refused too (a +host that needs one must be exempted in the policy, on purpose). The content +comparison no longer reads the negation of a class set once and never cleared +(`tendcf_cmdb_json_equal_*`); `tendcf_cmdb_problem_differs_` is defined +from the current texts and `..._uncomparable_` from a side that could not +be serialised. Measured on the pinned fork (check 11, debug log of a real +cf-agent): `tendcf_cmdb` is resolved twice, its vars are visited on every pass +(the `readjson` of `doc` included) and its classes are decided once, so a +later vars pass could have read a file the classes never compared. `doc` is +therefore read once (its promise is skipped once it exists) and every +comparison input derives from that snapshot. Check 11 pins both timings of a +file replaced during the run: before the read the comparison refuses ("content +differs", nothing runs); after it the policy never sees the replacement and +acts on the loaded data it checked. Reports print file-derived text only with +characters outside `[A-Za-z0-9._:@/-]` replaced by `?` and cut to a fixed +length. `tendcf-install.sh data` installs by atomic rename. Not checked: a file +replaced after the last read (no check can see it). On any mismatch it defines the one class `tendcf_cmdb_refused`, and `tendcf_interlocks` reports what differs From 1a8647f2982febad5384abd78647990b022c2de7 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 14:00:12 -0500 Subject: [PATCH 11/13] tests: no-verdict comparison probe (B holds $( in a pre_action element) and a 70-character key report case; both must fail on db1e73e's policy Co-Authored-By: Claude Sonnet 5.5 --- tests/policy/run.sh | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/policy/run.sh b/tests/policy/run.sh index c09ecf8..341006d 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -646,6 +646,21 @@ done check "uncomparable vars values: the object containers are not blamed" \ bash -c "! grep -Eq \"key 'tendcf_(interlock|service)' content (differs|could not be compared)\" '$out'" +# A key that is a plain identifier but longer than the 64 characters a report +# may show (Astra round 3, A2): refused as before, and the report line shows at +# most its first 64 characters. The 70-character key is the loader's own scalar +# case again, so it is refused for the same reason as 'scalar' above. +long_key="$(printf 'k%.0s' $(seq 1 70))" +long_head="${long_key:0:64}" +printf '{"vars":{"tendcf_interlock":{},"tendcf_service":{},"%s":"x"}}\n' "$long_key" >"$work/data/host_specific.json" +out="$work/xc-longkey.out" +"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true +check "long key: refusal reported" refusal_reported "$out" +check "long key: the report line shows its first 64 characters" \ + grep -Eq "key '$long_head' content (differs|could not be compared) between" "$out" +check "long key: no report line shows more than 64 characters of it" \ + bash -c "! grep -q 'key .${long_head}[A-Za-z0-9_]' '$out'" + # A `vars` key can preset any variable of the policy, the checker's own included # (adversary M1): a vars promise whose rvalue is a function call is skipped when # the variable already exists (verify_vars.c, "Already did this"), so a preset @@ -840,9 +855,27 @@ echo "info: tendcf_cmdb resolved $n_res times; vars 'doc' visited $n_doc times, check "probe (debug run): the mutation script ran" [ "$n_mut" -ge 1 ] check "probe (debug run): the file is read once (doc promise evaluated once)" [ "$n_doc" = 1 ] +# A comparison that has no verdict (adversary round 3, L1): B is A with one +# pre_action element changed to `/usr/bin/true$(nonexistent)`, swapped in before +# the check's read. strcmp() is skipped when an argument still holds `$(`, so +# neither a difference nor an equality is computed; the check must refuse anyway, +# name the key and run nothing. (The loader refuses `$(` in a value, so this can +# only be a file replaced after the loader's read, which is what the probe does.) +printf '%s\n' "${probe_a//\"\/usr\/bin\/true\"/\"\/usr\/bin\/true\$(nonexistent)\"}" > "$work/probe-B.json" +probe_start +out="$work/xc-probe-noverdict.out" +"$cf_agent" -K -I -D probe_before -f "$work/inputs/probe-before.cf" >"$out" 2>&1 || true +check "probe no verdict: the file was replaced" probe_replaced +check "probe no verdict: the replacement really holds a \$( element" \ + grep -qF "true\$(nonexistent)" "$work/data/host_specific.json" +check "probe no verdict: refused" refusal_reported "$out" +check "probe no verdict: the key is named" \ + grep -Eq "key 'tendcf_interlock' content (differs|comparison reached no verdict) between" "$out" +check "probe no verdict: nothing ran (neither the loaded nor the replaced pre_action)" no_actuation "$out" + if [ "$fail" != 0 ]; then for f in refused satisfied noshell-or noshell-semi argv-alphabet argv-meta relative flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 sysd6 nixos-shape nixos-tombstone \ - xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist xc-probe-before xc-probe-after \ + xc-malformed xc-nofile xc-novars xc-garbage xc-collision xc-classes xc-preset xc-preset-vars xc-uncomparable xc-checker-preset xc-checker-preset-keys xc-classes-hidden xc-augments xc-strlist xc-probe-before xc-probe-after xc-probe-noverdict xc-longkey \ xc-sim-control xc-sim-nointerlock xc-sim-noservice xc-sim-partial xc-sim-stray xc-sim-nofile xc-sim-changed-service xc-sim-changed-interlock \ sysd-sim-present sysd-sim-tomb; do [ -f "$work/$f.out" ] || continue From 49681a75f52a2e88921ac0348cef474341261a88 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 14:09:16 -0500 Subject: [PATCH 12/13] policy: a comparison with no verdict refuses (positive json_same and problem_noverdict classes, before tendcf_cmdb_refused); reports cap the key at 64 characters Co-Authored-By: Claude Sonnet 5.5 --- policy/tendcf_services.cf | 69 ++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/policy/tendcf_services.cf b/policy/tendcf_services.cf index d4c96d9..29f0508 100644 --- a/policy/tendcf_services.cf +++ b/policy/tendcf_services.cf @@ -90,8 +90,9 @@ # slist of strings (numbers read back as strings; a list of strings reads # back equal and is accepted). Equal ids with changed values, as a # `variables` entry would give, are caught. A comparison that cannot be -# computed is a problem of its own (`..._problem_uncomparable_`), not -# the absence of a difference; +# computed is a problem of its own (`..._problem_uncomparable_`, or +# `..._problem_noverdict_` when both texts exist but neither equality +# nor a difference could be evaluated), not the absence of a difference; # - every `data:variables.*` the loader installed must be a key of `vars` # in the file; # - nothing the loader installed may lie outside `data:variables.`: @@ -150,9 +151,13 @@ # the id lists are literal empty lists, which redefine a preset. # # Order and evaluation, as measured on the pinned fork (tests/policy/run.sh -# check 11, debug log of a real cf-agent): `tendcf_cmdb` is resolved twice, its -# vars are visited on every pass, the readjson of `doc` included, and its -# classes are decided once. The fork evaluates the vars and the classes of every +# check 11, debug run of a real cf-agent, the replaced-file case): `tendcf_cmdb` +# was resolved 2 times, its vars `doc` promise was visited 1 time (skipped once +# `doc` exists), `file_json[..]` 10 times and the class `tendcf_cmdb_refused` 2 +# times; the mutation script ran 1 time. So the vars are visited on every +# resolution, and a class is set once and never cleared, while a class that is +# still unset is evaluated again on the second resolution, from the same +# snapshot. The fork evaluates the vars and the classes of every # `bundle common` before the vars of any other bundle, independently of # `bundlesequence` (PolicyResolve and BundleResolve, libpromises/expand.c:846-871 # and 1145-1177), so `tendcf_cmdb_refused` is decided, on the first pass over @@ -169,7 +174,17 @@ # from a sticky "equal" class would stay false after a later mismatch. The # classes of the common bundle run in one pass in written order: # `tendcf_cmdb_refused` comes after every `tendcf_cmdb_problem_*` class; do not -# reorder them. +# reorder them. `tendcf_cmdb_json_same_` (the two texts are equal) is read +# only by `..._problem_noverdict_`, which refuses a comparison that gave +# neither "equal" nor "differs" (strcmp() is skipped when an argument still holds +# `$(`); it comes after the classes it reads and cannot hide a difference. +# +# One file on purpose: the fork resolves each file's bundles when it is loaded +# and the whole policy again afterwards (libpromises/loading.c), so `tendcf_cmdb` +# decides in the file it shares with the gated bundles, before their ids are +# first read. Were it split off, the literal empty `*_ids` lists under the +# refusal would still redefine the ids, so it would fail closed, but this is why +# the bundles live together. # # What is NOT guaranteed: the check compares the file with the loader's state at # the evaluation. A file replaced after the last evaluation, or by a promise @@ -286,6 +301,12 @@ bundle common tendcf_cmdb "key_canon[$(safe_keys)]" string => canonify("$(safe_keys)"); + # The key as a report shows it: at most 64 characters (a safe key is a plain + # identifier, so no character replacement is needed, but its length is not + # bounded). Lookups and class names keep using the full `safe_keys`. + "key_clean[$(safe_keys)]" + string => string_head("$(safe_keys)", 64); + "file_ids[$(safe_keys)]" slist => getindices("doc[vars][$(safe_keys)]"); @@ -382,6 +403,20 @@ bundle common tendcf_cmdb not(strcmp("$(file_json[$(safe_keys)])", "$(loaded_json[$(safe_keys)])"))); + # The two texts, computed now, are equal. Positive and set once, like every + # class here, and it is read only by the no-verdict class below, so it can + # only silence a no-verdict refusal; it never suppresses + # `..._problem_differs_` or `..._problem_uncomparable_`, which do not read + # it. The inputs are one snapshot (`doc` is read once, the loaded + # containers do not change), so the comparison gives the same result on + # every evaluation. + "tendcf_cmdb_json_same_$(key_canon[$(safe_keys)])" + expression => and(isvariable("data:variables.$(safe_keys)"), + isvariable("file_json[$(safe_keys)]"), + isvariable("loaded_json[$(safe_keys)]"), + strcmp("$(file_json[$(safe_keys)])", + "$(loaded_json[$(safe_keys)])")); + # A loaded container for which one side could not be serialised. Needs only # isvariable(), whose arguments are literal names, so it cannot be # suppressed by an undefined variable. @@ -390,6 +425,19 @@ bundle common tendcf_cmdb or(not(isvariable("file_json[$(safe_keys)]")), not(isvariable("loaded_json[$(safe_keys)]")))); + # Both containers serialised, and yet neither "equal" nor "differs" was + # computed: strcmp() is skipped (the call fails, so the class stays + # undefined) when an argument still holds `$(` or `${` after expansion, + # which a file replaced after the loader's read can give (the loader itself + # refuses `$(`, `${`, `@(`, `@{` in a key or a value). No verdict is not + # agreement: refuse. Written after the two classes it reads. + "tendcf_cmdb_problem_noverdict_$(key_canon[$(safe_keys)])" + expression => and(isvariable("data:variables.$(safe_keys)"), + isvariable("file_json[$(safe_keys)]"), + isvariable("loaded_json[$(safe_keys)]"), + not("tendcf_cmdb_json_same_$(key_canon[$(safe_keys)])"), + not("tendcf_cmdb_problem_differs_$(key_canon[$(safe_keys)])")); + "tendcf_cmdb_refused" expression => classmatch("tendcf_cmdb_problem_.*"); } @@ -502,13 +550,16 @@ bundle agent tendcf_interlocks "tendcf: CMDB cross-check: CFEngine loaded data:variables.$(tendcf_cmdb.unlisted_clean), which is not a key under 'vars' in $(tendcf_cmdb.file)"; "tendcf_cmdb_problem_partial_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.safe_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.safe_keys)])]"; + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.key_clean[$(tendcf_cmdb.safe_keys)])' loaded differently from $(tendcf_cmdb.file): in the file only: [$(tendcf_cmdb.unloaded_text[$(tendcf_cmdb.safe_keys)])]; loaded only: [$(tendcf_cmdb.unfiled_text[$(tendcf_cmdb.safe_keys)])]"; "tendcf_cmdb_problem_differs_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content differs between $(tendcf_cmdb.file) and the data CFEngine loaded"; + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.key_clean[$(tendcf_cmdb.safe_keys)])' content differs between $(tendcf_cmdb.file) and the data CFEngine loaded"; "tendcf_cmdb_problem_uncomparable_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: - "tendcf: CMDB cross-check: key '$(tendcf_cmdb.safe_keys)' content could not be compared between $(tendcf_cmdb.file) and the data CFEngine loaded (one side is not a container or list)"; + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.key_clean[$(tendcf_cmdb.safe_keys)])' content could not be compared between $(tendcf_cmdb.file) and the data CFEngine loaded (one side is not a container or list)"; + + "tendcf_cmdb_problem_noverdict_$(tendcf_cmdb.key_canon[$(tendcf_cmdb.safe_keys)])":: + "tendcf: CMDB cross-check: key '$(tendcf_cmdb.key_clean[$(tendcf_cmdb.safe_keys)])' content comparison reached no verdict between $(tendcf_cmdb.file) and the data CFEngine loaded (a text that cannot be evaluated, such as one holding a variable reference)"; "tendcf_cmdb_problem_sections":: "tendcf: CMDB cross-check: $(tendcf_cmdb.file) has top-level section '$(tendcf_cmdb.sections_clean)' besides 'vars'; only 'vars' is accepted (the loader would also apply 'variables' and 'classes')"; From 5a2228dbecebf8be3c5f92fee2c2e39924fdfbed Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 14:09:20 -0500 Subject: [PATCH 13/13] docs, tests: state the measured evaluation counts for the tested case (classes set once and never cleared, an unset class re-evaluated on the second resolution), why the bundles share one file, the no-verdict class; check-list header Co-Authored-By: Claude Sonnet 5.5 --- .../tendcf-validate-proposal-2026-11-03.md | 25 ++++++++++++++----- tests/policy/run.sh | 9 ++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/docs/architecture/tendcf-validate-proposal-2026-11-03.md b/docs/architecture/tendcf-validate-proposal-2026-11-03.md index da40776..5e5aeb0 100644 --- a/docs/architecture/tendcf-validate-proposal-2026-11-03.md +++ b/docs/architecture/tendcf-validate-proposal-2026-11-03.md @@ -1168,18 +1168,31 @@ tag (`classmatch`). tendcf ships no `def.json`, so augments are refused too (a host that needs one must be exempted in the policy, on purpose). The content comparison no longer reads the negation of a class set once and never cleared (`tendcf_cmdb_json_equal_*`); `tendcf_cmdb_problem_differs_` is defined -from the current texts and `..._uncomparable_` from a side that could not -be serialised. Measured on the pinned fork (check 11, debug log of a real -cf-agent): `tendcf_cmdb` is resolved twice, its vars are visited on every pass -(the `readjson` of `doc` included) and its classes are decided once, so a -later vars pass could have read a file the classes never compared. `doc` is +from the current texts, `..._uncomparable_` from a side that could not be +serialised, and `..._noverdict_` when both texts exist but neither +equality nor a difference could be evaluated (`strcmp()` is skipped when an +argument still holds `$(`, which a file replaced after the loader's read can +give; the positive `tendcf_cmdb_json_same_` it reads only silences that +one refusal and cannot hide a difference). Measured on the pinned fork (check +11, debug run of a real cf-agent, the replaced-file case, CI run 43626676907 +at 87a4b58): `tendcf_cmdb` was resolved 2 times, the `doc` promise visited 1 +time, `file_json[..]` 10 times and `tendcf_cmdb_refused` 2 times, and the +mutation script ran 1 time. A class is set once and never cleared; a class +still unset is evaluated again on the second resolution, from the same +snapshot. Its vars are visited on every resolution, so a later pass could have +read a file the classes never compared. `doc` is therefore read once (its promise is skipped once it exists) and every comparison input derives from that snapshot. Check 11 pins both timings of a file replaced during the run: before the read the comparison refuses ("content differs", nothing runs); after it the policy never sees the replacement and acts on the loaded data it checked. Reports print file-derived text only with characters outside `[A-Za-z0-9._:@/-]` replaced by `?` and cut to a fixed -length. `tendcf-install.sh data` installs by atomic rename. Not checked: a file +length (64 characters for a key, section name or id, whether or not it is a +plain identifier). The check and the gated bundles share one file because the +fork resolves each file when it is loaded and the whole policy again after; +were they split, the literal empty id lists under a refusal would still +redefine the ids, so the split would fail closed. `tendcf-install.sh data` +installs by atomic rename. Not checked: a file replaced after the last read (no check can see it). On any mismatch it defines the one diff --git a/tests/policy/run.sh b/tests/policy/run.sh index 341006d..fa233fc 100755 --- a/tests/policy/run.sh +++ b/tests/policy/run.sh @@ -70,7 +70,8 @@ # hidden from the file-based checks by such a preset; and an augments file # (`def.json` in inputs/) that defines a class and a variable. A `vars` # container that is a list of strings is the positive control: accepted, -# actuates. Every file of checks 2-8 and 6 is also a no-report control. +# actuates. A plain-identifier key longer than 64 characters is refused and +# its report line shows only the first 64. Every file of checks 2-8 and 6 is also a no-report control. # NB: the fork's loader skips a bad entry and installs the rest, but the # Mac dev build rejects the whole `vars` instead (the same fixture refuses # on both, as "nothing loaded"), so only the fork's CI legs see the @@ -95,7 +96,9 @@ # (refused, "content differs", nothing runs) and after it (the policy reads # the file once, so not refused, the loaded pre_action runs and the replaced # one does not); a debug run counts the reads (the doc promise is evaluated -# once) and prints the other counts (`info:`). +# once) and prints the other counts (`info:`). A third case swaps in a file +# whose pre_action element holds `$(` (strcmp() is skipped, so the comparison +# has no verdict): refused as "reached no verdict", nothing runs. # # The pre_action commands are /usr/bin/true and /usr/bin/false, which exist # on Linux and macOS alike; /bin/true does not exist on macOS, and a missing @@ -870,7 +873,7 @@ check "probe no verdict: the replacement really holds a \$( element" \ grep -qF "true\$(nonexistent)" "$work/data/host_specific.json" check "probe no verdict: refused" refusal_reported "$out" check "probe no verdict: the key is named" \ - grep -Eq "key 'tendcf_interlock' content (differs|comparison reached no verdict) between" "$out" + grep -q "key 'tendcf_interlock' content comparison reached no verdict between" "$out" check "probe no verdict: nothing ran (neither the loaded nor the replaced pre_action)" no_actuation "$out" if [ "$fail" != 0 ]; then