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>
112 lines
4.1 KiB
C++
112 lines
4.1 KiB
C++
#include "evo/persistence/region_lease.hpp"
|
|
#include <algorithm>
|
|
#include <initializer_list>
|
|
#if defined(__linux__)
|
|
#include <time.h>
|
|
#endif
|
|
|
|
namespace evo::persistence {
|
|
namespace {
|
|
using std::chrono::milliseconds;
|
|
constexpr milliseconds minimum_duration{1000};
|
|
constexpr milliseconds maximum_duration{60000};
|
|
constexpr milliseconds maximum_io{30000}; // Postgres adapter deadline bound.
|
|
} // namespace
|
|
|
|
bool valid(const LeasePolicy& p) noexcept {
|
|
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 &&
|
|
(p.renew_interval + p.renew_timeout) * 2 <= usable && p.commit_timeout < usable;
|
|
}
|
|
|
|
LeaseClock::time_point LeaseClock::now() noexcept {
|
|
#if defined(__linux__)
|
|
timespec ts{};
|
|
// CLOCK_BOOTTIME cannot fail with a valid clock id and pointer.
|
|
static_cast<void>(clock_gettime(CLOCK_BOOTTIME, &ts));
|
|
return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}};
|
|
#else
|
|
// Storage hosts are Linux-only; elsewhere suspend behavior is unqualified.
|
|
return time_point{
|
|
std::chrono::duration_cast<duration>(std::chrono::steady_clock::now().time_since_epoch())};
|
|
#endif
|
|
}
|
|
|
|
bool LeaseAuthority::acquired(const LeaseGrant& grant, LeaseClock::time_point sent,
|
|
LeaseClock::time_point received) noexcept {
|
|
// 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) {
|
|
fence();
|
|
return false;
|
|
}
|
|
const auto deadline = sent + grant.duration - policy_.safety_margin;
|
|
if (received >= deadline) {
|
|
fence();
|
|
return false;
|
|
}
|
|
grant_ = grant;
|
|
highest_epoch_ = grant.epoch;
|
|
last_sent_ = sent;
|
|
deadline_ = deadline;
|
|
fenced_ = false;
|
|
return true;
|
|
}
|
|
|
|
bool LeaseAuthority::renewed(std::uint64_t epoch, milliseconds granted, LeaseClock::time_point sent,
|
|
LeaseClock::time_point received) noexcept {
|
|
if (!grant_ || fenced_ || epoch != grant_->epoch || granted != policy_.duration ||
|
|
sent < last_sent_ || received < sent || received >= deadline_) {
|
|
fence();
|
|
return false;
|
|
}
|
|
// Database expiry is its execution time plus granted, which is after sent.
|
|
deadline_ = sent + granted - policy_.safety_margin;
|
|
last_sent_ = sent;
|
|
return true;
|
|
}
|
|
|
|
void LeaseAuthority::renewal_unknown(milliseconds requested, LeaseClock::time_point sent) noexcept {
|
|
if (!grant_ || fenced_)
|
|
return;
|
|
// 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);
|
|
}
|
|
|
|
void LeaseAuthority::fence() noexcept {
|
|
fenced_ = true;
|
|
}
|
|
|
|
LeaseState LeaseAuthority::state(LeaseClock::time_point now) const noexcept {
|
|
if (!grant_)
|
|
return LeaseState::absent;
|
|
if (fenced_ || now >= deadline_)
|
|
return LeaseState::fenced;
|
|
return now >= last_sent_ + policy_.renew_interval ? LeaseState::renew_due : LeaseState::active;
|
|
}
|
|
|
|
bool LeaseAuthority::may_originate(LeaseClock::time_point now) const noexcept {
|
|
const auto s = state(now);
|
|
return s == LeaseState::active || s == LeaseState::renew_due;
|
|
}
|
|
|
|
bool LeaseAuthority::may_begin_commit(LeaseClock::time_point now) const noexcept {
|
|
return may_originate(now) && now + policy_.commit_timeout <= deadline_;
|
|
}
|
|
} // namespace evo::persistence
|