Search and contents

Activity Log Tamper-Evidence: hash-chained entries, syslog checkpoints

This feature detects an edit, deletion or reordering of activity_log history made by a database administrator who deliberately disables the append-only guard triggers (GUARD_SQL in engine/activity_log.py) and modifies rows directly.

The guard prevents accidents and stray SQL, even from a superuser session. It does not stop a determined insider, and this feature does not either. It detects one, after the fact.

Scope: per node instance, not one global chain

Each node instance's entries form their own independent hash chain, identified by node_instance_id. There is no single chain across the whole activity_log table.

Concurrency locking, syslog checkpoints and verify_chain() are all scoped the same way. A development layout may share one physical Postgres database across node types, but that is a separate characteristic of the table. This feature neither changes it nor depends on it.

1. How a chain is built

Every ActivityLog.write() call does the following:

  1. Locks the node instance. It takes a Postgres advisory lock scoped to node_instance_id, the same idiom ensure_schema() uses for the whole table, but narrower. Concurrent writers for the same node instance are serialized, across threads and across the separate OS processes this codebase runs against one shared database.
  2. Reads the head. It reads the current head hash from activity_log_chain_heads, a small side table with one row per node instance. NULL or absent means genesis: the first chained entry for that node.
  3. Computes the entry's hash. chain_hash is a SHA-256 over the entry's content fields plus the head hash it links from (chain_prev_hash).
  4. Inserts and moves the head. It inserts the entry with both chain columns set and upserts the new head, all in the same transaction. A failure anywhere in this sequence fails the whole write closed. No code path lands a content row with the chain fields skipped.

Why ordering comes from the chain

A chain is reconstructed from the chain_prev_hash → chain_hash pointers themselves, never from occurred_at or id, the same way git commits or a blockchain link.

This is what makes an edited or reordered entry detectable at all. occurred_at is one of the fields the hash covers, so tampering with it breaks that entry's own hash immediately, regardless of the order a query happens to return rows in.

Rows from before the chain existed

A row written before this feature landed has chain_hash and chain_prev_hash both NULL. verify_chain() reports it as unchained, counts it separately, and never silently treats it as verified.

2. Syslog checkpoints

After each write commits, ActivityLog._emit_chain_checkpoint publishes the new head to the local syslog socket (/dev/log), fire-and-forget. That socket is the standard Unix domain socket every Linux host already has, so it needs no new config and no new dependency.

Whatever the site's own syslog forwards to (a SIEM, a remote collector) becomes the off-box, harder-to-forge copy of the chain's current head.

Why every write

The checkpoint runs on every write, not on a schedule. The head moves with every append. Checkpointing less often leaves a rolling window, as long as the checkpoint interval, in which an attacker could tamper with recent entries and regenerate a self-consistent suffix with nothing external to contradict it. Activity Log writes happen at Task-run frequency, not high volume, so the per-write overhead is negligible.

Message body

The body is JSON, matching the codebase's structured-over-free-text convention elsewhere (JSONB fields, the MQTT publish's own JSON payload):

{
  "event": "activity_log_chain_checkpoint",
  "node_type": "MES",
  "node_instance_id": "mes-line-3",
  "entry_id": "5930c9f3-...",
  "occurred_at": "2026-09-29T13:13:47.362758+00:00",
  "chain_hash": "cd0b3fe3..."
}

Failure behavior and test injection

  • A syslog emit failure never fails the write. A missing /dev/log or a full disk makes no difference, because the Activity Log write has already committed by the time the checkpoint runs. This is the same "never raises" contract that publish_activity_log_event in engine/mqtt.py keeps for a down MQTT broker.
  • write() accepts an optional chain_checkpoint_logger argument that replaces the default syslog-backed logger. It is the same injection shape as SidecarProcess's process_factory=, and lets tests inject a capturing logger instead of touching a real /dev/log.

3. Verifying a chain

poetry run python -m engine.activity_log_verify verify --node-instance-id mes-line-3
poetry run python -m engine.activity_log_verify verify   # every node with a chained entry

--node-instance-id is repeatable. Each checked node prints one line:

[OK] <node_instance_id>: N chained, M unchained, head=<hash>

A failing node prints [FAILED] ... with the finding on the next line. head= is omitted when the node has no head.

verify_chain(node_instance_id, expected_head=None) in engine/activity_log.py walks the chain and reports the first problem it finds:

FindingMeaning
fork Two entries both claim the same chain_prev_hash. Caught before the walk starts.
broken An entry's stored chain_hash doesn't match its recomputed content hash: an edited field.
gap A chained entry was never reached walking forward from genesis: a deleted entry broke the forward link.
head mismatch The walk's final hash disagrees with the value stored in activity_log_chain_heads: the side table and the reconstructed chain disagree.
orphaned chain Chained rows exist for the node but none is a genesis entry (one with no chain_prev_hash), so there is nowhere to start the walk.
externally recorded checkpoint mismatch The walk's head differs from a value recorded off-box. Checked only when --expected-head is supplied.

Exit code: 0 if every checked node's chain is ok, 1 if any fails.

--expected-head: the check that catches a wholesale rewrite

--expected-head NODE_INSTANCE_ID=HEXHASH (repeatable) is how an operator feeds back a checkpoint value copied from the site's own syslog or SIEM record. The tool cannot fetch it itself, because where it ends up is site-specific.

This is the one check that can catch a wholesale chain rewrite. A fully regenerated but internally self-consistent chain passes every other check in the table above: every entry recomputes correctly, there is no fork or gap, and the side table agrees with the reconstructed head. None of those checks compares against anything outside the database. Only a value recorded off-box, at some point before the rewrite, can catch it.

What this does not do

  • It does not prevent tampering, only detects it. A determined insider with database access who also disables the guard triggers can edit history. This makes the edit discoverable, not impossible.
  • It does not fetch the externally recorded head. An operator supplies --expected-head by hand, copied from wherever the site's syslog ends up. The tool has no credentials or network path to any site's SIEM or log collector.
  • It offers no revocation or undo. verify_chain reports what broke. Fixing it, if that is even possible, is outside this feature's scope. The append-only guard likewise offers no way to "un-guard" other than the documented prune transaction in database-reference.md.

Pitfalls

  • A syslog checkpoint proves a write happened, not when. The occurred_at field inside the checkpoint payload is caller-supplied (the same field the chain hash covers), not the syslog daemon's own receipt timestamp. If the distinction matters for an audit, cross-referencing the receiving system's own arrival time is the site's concern.
  • activity_log_chain_heads is guarded only by ordinary application logic, not a database trigger. GUARD_SQL covers only activity_log, so an insider with the access this feature's threat model already assumes could edit the side table directly. verify_chain's head-mismatch check catches exactly that: if the side table and the entry chain disagree, the disagreement is itself the finding, and neither side silently wins.
  • Dev and test Postgres instances accumulate test-*-prefixed chained entries across local test runs. The tamper-detection tests in tests/test_activity_log.py deliberately corrupt or delete rows using the guard-trigger-disable pattern described above. This is harmless (random-UUID node instance IDs, no real data). It is worth knowing if verify run without --node-instance-id against a shared dev database reports more FAILED lines than you expect.

Release: v0.11.0