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
+63
View File
@@ -29,4 +29,67 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
\ir 0001_region_bootstrap.sql
\ir 0002_region_fencing.sql
\endif
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
-- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC
-- grants or SET-able memberships would bypass the fenced functions. Checks the
-- runtime group and every role that is (transitively) a member of it.
SELECT pg_catalog.set_config('evolution.runtime_role', :'runtime_role', true);
DO $$
DECLARE
runtime oid := (SELECT r.oid FROM pg_catalog.pg_roles r
WHERE r.rolname = pg_catalog.current_setting('evolution.runtime_role'));
bad text;
BEGIN
IF runtime IS NULL THEN
RAISE EXCEPTION 'runtime role does not exist';
END IF;
WITH RECURSIVE closure(oid) AS (
SELECT runtime
UNION
SELECT m.member FROM pg_catalog.pg_auth_members m JOIN closure c ON m.roleid = c.oid
), fenced(oid) AS (VALUES
('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer)'::regprocedure::oid),
('evolution.renew_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64,integer)'::regprocedure::oid),
('evolution.release_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
('evolution.load_region_head(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.advance_region_head(evolution.u64,evolution.u64,uuid,evolution.u64,evolution.u64,uuid,bytea,uuid,bytea,evolution.u64)'::regprocedure::oid)
), violations(role_oid, reason) AS (
SELECT r.oid, 'privileged attribute or owner identity' FROM pg_catalog.pg_roles r
JOIN closure c ON c.oid = r.oid
WHERE r.rolsuper OR r.rolcreaterole OR r.rolcreatedb OR r.rolreplication
OR r.rolbypassrls OR r.rolname = current_user
UNION ALL
-- Any membership outside the runtime closure (inherited or SET-able).
SELECT m.member, 'membership outside the runtime group' FROM pg_catalog.pg_auth_members m
JOIN closure c ON c.oid = m.member
WHERE m.roleid NOT IN (SELECT oid FROM closure)
UNION ALL
SELECT c.oid, 'table privilege in evolution schema' FROM closure c
CROSS JOIN pg_catalog.pg_class t
WHERE t.relnamespace = 'evolution'::regnamespace AND t.relkind IN ('r', 'p', 'v', 'm', 'S')
AND pg_catalog.has_table_privilege(c.oid, t.oid,
'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER,MAINTAIN')
UNION ALL
SELECT c.oid, 'CREATE on schema or database' FROM closure c
WHERE pg_catalog.has_schema_privilege(c.oid, 'evolution', 'CREATE')
OR pg_catalog.has_database_privilege(c.oid, pg_catalog.current_database(), 'CREATE')
OR EXISTS (SELECT 1 FROM pg_catalog.pg_namespace n WHERE n.nspname = 'public'
AND pg_catalog.has_schema_privilege(c.oid, n.oid, 'CREATE'))
UNION ALL
-- Only the five guarded functions are executable; provisioning/internal are not.
SELECT c.oid, 'EXECUTE outside the fenced function set' FROM closure c
CROSS JOIN pg_catalog.pg_proc p
WHERE p.pronamespace = 'evolution'::regnamespace
AND p.oid NOT IN (SELECT oid FROM fenced)
AND pg_catalog.has_function_privilege(c.oid, p.oid, 'EXECUTE')
UNION ALL
SELECT runtime, 'missing EXECUTE on a fenced function' FROM fenced f
WHERE NOT pg_catalog.has_function_privilege(runtime, f.oid, 'EXECUTE')
)
SELECT pg_catalog.string_agg(DISTINCT v.reason, '; ') INTO bad FROM violations v;
IF bad IS NOT NULL THEN
-- Reasons only; no role names or connection details are printed.
RAISE EXCEPTION 'runtime role audit failed: %', bad;
END IF;
END $$;
COMMIT;