Search and contents

Tampering with the Activity Log, and what the chain shows afterwards

A Node's Activity Log is the record of what it did, and the database refuses to change it: an update, a delete or a truncate is rejected. This page is the other half. A person who has database access and turns that refusal off themselves can still edit a record, and the log is built so that the edit shows afterwards. You do it, on a throwaway database the practice site made, and read what the check reports. You also see what the check does not catch.

Practice only, and only on the practice site's own database. The steps below switch off a guard and edit a log. Run them against the database a practice site made (practice_erp) and nowhere else: never against a Node's own database, and never against one you would want to read again. The site never does this itself; the person running the page does. reset and down drop the database, which is how the tampering is undone.

Every value on this page is invented. The site, its Nodes and what they logged are made up for this walk-through.

This is test tooling, not part of the public API. The site script and its verify-log command may change in any release, and the stability policy (public-api-stability.md) does not cover them. The feature itself, hash-chained entries with a checkpoint written to the system log, is described in activity-log-tamper-evidence.md. The tests that pin it are tests/test_activity_log_append_only.py, tests/test_activity_log.py and tests/test_activity_log_verify.py.

What you need

  • A running practice site, as in A Node stopped and started. No model is asked by anything on this page.
  • A Postgres superuser you can reach, the way the site script reaches it: sudo -n -u postgres psql, or the command in KINDRED_PRACTICE_PSQL_ADMIN. Switching a trigger off needs the table's owner or a superuser.
  • A system log the Nodes write to, for Step 2. A Node publishes each entry's chain hash to the local syslog socket (/dev/log) as it writes. On a host where that socket leads to the journal, the journal holds them, and reading it needs sudo. Without a socket nothing is written, and Step 2 is skipped.

Run the commands from the repository root, in one shell, after this once:

SITE="${KINDRED_PRACTICE_DIR:-$HOME/.kindred-practice-site}"
export KINDRED_PRACTICE_DIR="$SITE"
PS=deployment/practice_site.sh
PSQL="${KINDRED_PRACTICE_PSQL_ADMIN:-sudo -n -u postgres psql}"
ERP_DB=practice_erp
sql() { # run psql on the practice site's ERP database as the Postgres superuser, and on nothing else
  case "$ERP_DB" in
    practice_*) ;;
    *) echo "refusing: $ERP_DB is not a practice site's database" >&2; return 1 ;;
  esac
  $PSQL -d "$ERP_DB" "$@"
}
head_of() { # the head of ERP's chain, as verify-log prints it
  $PS verify-log erp | sed -n 's/.*head=\([0-9a-f]\{64\}\).*/\1/p'
}

Step 1: read ERP's chain, and write its head down somewhere else

Each Node's entries form a chain of their own: an entry's hash covers its content and the hash of the one before it. verify-log checks one Node's chain in that Node's own database. It runs the verifier with that Node's settings and nothing of your shell's: a bare run of the verifier reads the database of whatever checkout you run it from, which on a host that also runs a real Node is that Node's.

$PS verify-log erp
[OK] erp: 11 chained, 0 unchained, head=5046a7bb83ee5b6e65add7460b5a5fc0515386c5882b2fd6d31b7973431fd576

The head is the hash of the newest entry. Copy it somewhere the database does not hold. This is the step that makes a later rewrite of the whole chain detectable, and it only works if the copy is out of reach of whoever rewrites:

head_of > "$SITE/erp-head.txt"
cat "$SITE/erp-head.txt"

A head is good until the Node writes again: the next entry moves it. So a copy is only useful for a log that has not written since, or as the latest of a stream of them, which is what Step 2 is.

Step 2: where a head from outside comes from

The Node does not wait for you to copy it. Every write publishes the new head to the local syslog socket, so whatever the site's syslog forwards to (a collector, a SIEM) holds a copy the database's own administrator cannot reach. On this host the socket leads to the journal:

sudo -n journalctl --no-pager -o cat -n 3000 |
  grep activity_log_chain_checkpoint | grep '"node_instance_id": "erp"' | tail -1 |
  python3 -c 'import json, sys; print(json.loads(sys.stdin.read())["chain_hash"])'
head_of
5046a7bb83ee5b6e65add7460b5a5fc0515386c5882b2fd6d31b7973431fd576
5046a7bb83ee5b6e65add7460b5a5fc0515386c5882b2fd6d31b7973431fd576

The latest checkpoint for ERP is the head the database holds. Pass a head from there, or from your own copy of Step 1, to verify-log and it compares them as well:

$PS verify-log erp --expected-head "erp=$(cat "$SITE/erp-head.txt")"
[OK] erp: 11 chained, 0 unchained, head=5046a7bb83ee5b6e65add7460b5a5fc0515386c5882b2fd6d31b7973431fd576

The tool cannot fetch a head itself, because where it ends up is the site's, and a checkpoint proves that a write happened, not when: the time inside it is the one the Node wrote.

Step 3: the guard refuses

Before anything is switched off, an edit is refused by the database, even from a superuser:

sql -c "UPDATE activity_log SET outcome = 'x' WHERE id = (SELECT id FROM activity_log ORDER BY occurred_at LIMIT 1)" 2>&1 | head -1
sql -At -c "SELECT tgname, tgenabled FROM pg_trigger WHERE tgrelid = 'activity_log'::regclass AND NOT tgisinternal ORDER BY tgname"
ERROR:  activity_log is append-only: UPDATE is not allowed
activity_log_no_truncate|O
activity_log_no_update_delete|O

The guard is two triggers, one for updates and deletes and one for truncates. O means enabled. It stops accidents and stray SQL. It does not stop a person with the access you are about to use.

Step 4: switch the guard off, edit one entry, switch it back on

The command, and what it changes. The first statement switches off the trigger that refuses updates and deletes on activity_log in the practice site's practice_erp database. The second changes the outcome of ERP's oldest entry. The third switches the trigger back on, so the database looks as it did, and the edit stays.

sql -c "ALTER TABLE activity_log DISABLE TRIGGER activity_log_no_update_delete"
sql -c "UPDATE activity_log SET outcome = 'edited by hand' WHERE id = (SELECT id FROM activity_log WHERE node_instance_id = 'erp' ORDER BY occurred_at LIMIT 1)"
sql -c "ALTER TABLE activity_log ENABLE TRIGGER activity_log_no_update_delete"
sql -At -c "SELECT tgname, tgenabled FROM pg_trigger WHERE tgrelid = 'activity_log'::regclass AND NOT tgisinternal ORDER BY tgname"
ALTER TABLE
UPDATE 1
ALTER TABLE
activity_log_no_truncate|O
activity_log_no_update_delete|O

Both triggers read O again. Nothing in the database now says an edit was made, and a quick look at the guard would find nothing wrong.

Step 5: verify

$PS verify-log erp
[FAILED] erp: 11 chained, 0 unchained
  broken: entry 53273954-f392-496f-8ec4-6bab759f5673's stored chain_hash doesn't match its own recomputed content hash - edited after being written

The command exits 1, and names the entry whose stored hash no longer matches its content. The entry's id is different in every run. The other Nodes' logs are untouched, which verify-log mes shows, and putting the guard back did not undo the edit: the break is there until the database is made again.

Give it the head you wrote down before the edit, and it still stops at the first finding. It walks the chain before it compares a head, so a chain that is already broken is reported as broken:

$PS verify-log erp --expected-head "erp=$(cat "$SITE/erp-head.txt")"

The guard is working again, which is the last thing to check before the page is done:

sql -c "UPDATE activity_log SET outcome = 'x' WHERE id = (SELECT id FROM activity_log ORDER BY occurred_at LIMIT 1)" 2>&1 | head -1

Step 6: undo it

$PS reset
$PS verify-log erp

reset drops and makes the Nodes' databases again (an Activity Log cannot be emptied in place), so the edited entry is gone and the new chain verifies. On the run shown above it printed [OK] erp: 9 chained, 0 unchained, head=....

What the check detects, and what it does not

It detects, after the fact, an edit, a deletion or a reordering made by someone who turned the guard off. An edited field breaks that entry's own hash (shown above). A deleted entry leaves a gap that the walk from the first entry cannot cross. Two entries that claim to follow the same one are a fork. The head the walk ends on is compared with the one the database keeps on the side, and a disagreement is itself the finding. With a head from outside, it also detects a chain that was rewritten from the point of the edit, entry by entry, with every hash recomputed: that chain is internally consistent, and nothing but a value recorded off the box before the rewrite can contradict it.

It does not prevent anything. The guard stops accidents and stray SQL, and a person with database access can turn it off, as you did. The feature makes an edit discoverable, not impossible.

It does not catch a determined insider who also removes the outside copy. Someone who can switch the guard off, rewrite the whole chain and the head the database keeps on the side, and also reach the system log or the copy you wrote down, can leave a log that verifies. The check is only as strong as the place the head was copied to. A fully regenerated chain with no head from outside passes every check.

It does not say who, or why. The report names an entry and says it was edited. It does not name the person, and it does not say what the entry used to read.

Only the edit was run by hand. A deleted entry, a fork, a chain regenerated whole and caught by --expected-head, and a side table edited to match, are pinned by the tests named at the top (test_verify_exits_nonzero_and_reports_the_finding_on_a_broken_chain and the cases beside it) and were not done on the site. A regenerated chain was not run by hand at all.

What this does not show

  • A Node that detects a bad peer's content. The chain says a Node's own record was changed after it was written. It says nothing of what a peer told it.
  • A person who leaves, or a token that is revoked. The revocation cascade is not run by hand on this site.
  • The grounds of an edit. The Node does not know whether a change was right.
  • Retention. An Activity Log can be pruned in a documented transaction that is not tampering and is not run here (database-reference.md).
  • A real site's syslog. The journal of one host stands in for a collector that the database's administrator cannot reach.

Limits

  • verify-log is practice-only and read-only. It reports; it repairs nothing and offers no undo.
  • A head is good until the Node writes again. Compare it against the latest checkpoint, or against a log that has not written since.
  • The side table of heads is guarded by ordinary application logic, not a trigger. An edit to it is caught because it then disagrees with the chain, and that disagreement is itself the finding.
  • A log written before the chain existed is reported as unchained, and never treated as verified.
  • One Node's database is checked at a time, and each Node's entries form a chain of their own. A database that holds more than one Node's entries (the MES's also holds the management service's) shows one line for each.

What the test checks

tests/test_activity_log_verify.py pins the verifier: an untouched chain is OK, an edited entry is FAILED with the finding, a head that does not match is reported and exits non-zero, and omitting the Node checks every chain. tests/test_activity_log_append_only.py pins the guard: the database itself rejects an update, a delete and a truncate. tests/test_practice_site_verify_log.py pins the command: it reads the Node's own database whatever the caller's environment holds, prints no password, and refuses a malformed head.

tests/test_break_it_pages.py runs this page's setup and every recipe above that it can, against a bare site directory and a throwaway database named as a practice site's is: the same statements, with the guard switched off, an entry edited and the guard back on, and the same two verify-log readings. It also pins that the page's sql function refuses a database that is not a practice site's. The block that reads the host's journal is not run.

Release: v0.11.0