Audit effective runtime privileges and bound lease policy arithmetic

Review P1: the v2 attribute-only runtime role check accepted a login that
inherits pg_write_all_data, which can UPDATE lease rows directly and
bypass fencing. apply.sql now audits, on every migrate run, the runtime
group and all transitive members: privileged attributes, any membership
outside the group (inherited or SET-able), table privileges in evolution
(including PUBLIC), CREATE rights and EXECUTE beyond the five fenced
functions. 0002 and its checksum are unchanged. CI reproduces the bypass
and proves each configuration is rejected and rolled back.

Review P2: valid(LeasePolicy) bounds every term by the duration before
arithmetic; LeaseAuthority validates durations before deadline math and
renewal_unknown ignores impossible (>60s) and fences non-positive requests.
Extreme min/max tests reproduce the old UBSan overflow and now pass.

Validated: Windows CTest 55/55; WSL Release, clang-tidy and ASan/UBSan
private-PostgreSQL suites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-28 13:50:44 -04:00
co-authored by Claude Opus 5.5
parent 2496649a9b
commit 07116446c6
8 changed files with 197 additions and 17 deletions
+29
View File
@@ -44,6 +44,35 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha"
test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1
PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh"
test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2
# Inherited privileges bypass fencing, so every migrate run audits effective rights.
psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_bad_runtime NOLOGIN IN ROLE pg_write_all_data'
if (export PGDATABASE=migration_rollback EVO_PG_RUNTIME_ROLE=evo_bad_runtime; \
bash "$root/tools/migrate-postgres.sh"); then
echo 'Expected inherited-privilege runtime rejection' >&2; exit 1
fi
test "$(PGDATABASE=migration_rollback psql -XAtc "SELECT count(*) FROM pg_namespace WHERE nspname='evolution'")" = 0
expect_audit_failure() {
psql -X -v ON_ERROR_STOP=1 -c "$1"
if bash "$root/tools/migrate-postgres.sh"; then
echo "Expected runtime audit failure after: $1" >&2; exit 1
fi
psql -X -v ON_ERROR_STOP=1 -c "$2"
}
# The reviewed scenario: the login inherits pg_write_all_data and can bypass the guard.
psql -X -v ON_ERROR_STOP=1 -c 'GRANT pg_write_all_data TO evo_runtime_login'
PGUSER=evo_runtime_login psql -X -v ON_ERROR_STOP=1 \
-c 'UPDATE evolution.region_leases SET epoch = 99 WHERE false' > /dev/null
psql -X -v ON_ERROR_STOP=1 -c 'REVOKE pg_write_all_data FROM evo_runtime_login'
expect_audit_failure 'GRANT pg_write_all_data TO evo_runtime_login' \
'REVOKE pg_write_all_data FROM evo_runtime_login'
expect_audit_failure 'GRANT pg_write_all_data TO evo_runtime_test WITH INHERIT FALSE' \
'REVOKE pg_write_all_data FROM evo_runtime_test'
expect_audit_failure 'GRANT UPDATE ON evolution.region_heads TO PUBLIC' \
'REVOKE UPDATE ON evolution.region_heads FROM PUBLIC'
expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.check_region_lease(evolution.u64, evolution.u64, uuid, evolution.u64) TO evo_runtime_test' \
'REVOKE EXECUTE ON FUNCTION evolution.check_region_lease(evolution.u64, evolution.u64, uuid, evolution.u64) FROM evo_runtime_test'
expect_audit_failure 'ALTER ROLE evo_runtime_login CREATEDB' 'ALTER ROLE evo_runtime_login NOCREATEDB'
bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again.
# Unknown future versions and checksum drift must not be repaired silently.
psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=3 WHERE version=2'
if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi