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
+23 -8
View File
@@ -1,5 +1,6 @@
#include "evo/persistence/region_lease.hpp"
#include <algorithm>
#include <initializer_list>
#if defined(__linux__)
#include <time.h>
#endif
@@ -13,11 +14,13 @@ constexpr milliseconds maximum_io{30000}; // Postgres adapter deadline bound.
} // namespace
bool valid(const LeasePolicy& p) noexcept {
const milliseconds zero{};
if (p.duration < minimum_duration || p.duration > maximum_duration ||
p.renew_interval <= zero || p.safety_margin <= zero || p.renew_timeout <= zero ||
p.commit_timeout <= zero || p.renew_timeout > maximum_io || p.commit_timeout > maximum_io ||
p.safety_margin >= p.duration)
if (p.duration < minimum_duration || p.duration > maximum_duration)
return false;
// Bound every term by the (<=60s) duration before any arithmetic can overflow.
for (const auto term : {p.renew_interval, p.safety_margin, p.renew_timeout, p.commit_timeout})
if (term <= milliseconds{} || term >= p.duration)
return false;
if (p.renew_timeout > maximum_io || p.commit_timeout > maximum_io)
return false;
const auto usable = p.duration - p.safety_margin;
return p.safety_margin * 10 >= p.duration &&
@@ -39,9 +42,14 @@ LeaseClock::time_point LeaseClock::now() noexcept {
bool LeaseAuthority::acquired(const LeaseGrant& grant, LeaseClock::time_point sent,
LeaseClock::time_point received) noexcept {
const auto deadline = sent + grant.duration - policy_.safety_margin;
// The duration equals the validated policy before it enters time arithmetic.
if (!valid(policy_) || grant.epoch == 0 || grant.epoch <= highest_epoch_ || grant.frame == 0 ||
grant.duration != policy_.duration || received < sent || received >= deadline) {
grant.duration != policy_.duration || received < sent) {
fence();
return false;
}
const auto deadline = sent + grant.duration - policy_.safety_margin;
if (received >= deadline) {
fence();
return false;
}
@@ -69,7 +77,14 @@ bool LeaseAuthority::renewed(std::uint64_t epoch, milliseconds granted, LeaseClo
void LeaseAuthority::renewal_unknown(milliseconds requested, LeaseClock::time_point sent) noexcept {
if (!grant_ || fenced_)
return;
deadline_ = std::min(deadline_, sent + requested - policy_.safety_margin);
// The database rejects durations outside 1..60s, so such a request cannot have
// changed expiry. A non-positive request is a caller error: fence.
if (requested <= milliseconds{}) {
fence();
return;
}
if (requested <= maximum_duration)
deadline_ = std::min(deadline_, sent + requested - policy_.safety_margin);
last_sent_ = std::max(last_sent_, sent);
}