Files
aevolution/migrations/apply.sql
T
dingoandClaude Opus 5.5 07116446c6 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>
2026-09-28 13:50:44 -04:00

96 lines
4.7 KiB
PL/PgSQL

\set ON_ERROR_STOP on
BEGIN;
SET LOCAL search_path = pg_catalog;
SET LOCAL statement_timeout = '10s';
SET LOCAL lock_timeout = '5s';
SELECT pg_catalog.pg_advisory_xact_lock(71420001);
SELECT current_setting('server_version_num')::int = 180006 AS supported \gset
\if :supported
\else
\echo 'Unsupported PostgreSQL version; expected 18.6'
DO $$ BEGIN RAISE EXCEPTION 'unsupported PostgreSQL version'; END $$;
\endif
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') AS exists \gset
\if :exists
-- Only an exact prefix of the checked-in history is admitted. Unknown or
-- partial/foreign schemas fail closed; no IF NOT EXISTS repair of foreign data.
SELECT coalesce(string_agg(version || ':' || sha256, ',' ORDER BY version), '')
AS history FROM evolution.schema_migrations \gset
SELECT :'history' = '1:' || :'migration_sha' AS at_v1,
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' AS at_v2 \gset
\if :at_v2
\elif :at_v1
\ir 0002_region_fencing.sql
\else
\echo 'Unknown or changed migration history'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
\endif
\else
\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;