123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- import "helpers/helpers.spec"
- import "methods/IAccessControlDefaultAdminRules.spec"
- import "methods/IAccessControl.spec"
- import "AccessControl.spec"
- use rule onlyGrantCanGrant filtered {
- f -> f.selector != acceptDefaultAdminTransfer().selector
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Helpers │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- function max_uint48() returns mathint {
- return (1 << 48) - 1;
- }
- function nonZeroAccount(address account) returns bool {
- return account != 0;
- }
- function timeSanity(env e) returns bool {
- return
- e.block.timestamp > 0 && // Avoids 0 schedules
- e.block.timestamp + defaultAdminDelay(e) < max_uint48();
- }
- function delayChangeWaitSanity(env e, uint48 newDelay) returns bool {
- return e.block.timestamp + delayChangeWait_(e, newDelay) < max_uint48();
- }
- function isSet(uint48 schedule) returns bool {
- return schedule != 0;
- }
- function hasPassed(env e, uint48 schedule) returns bool {
- return schedule < e.block.timestamp;
- }
- function min(uint48 a, uint48 b) returns mathint {
- return a < b ? a : b;
- }
- function increasingDelaySchedule(env e, uint48 newDelay) returns mathint {
- return e.block.timestamp + min(newDelay, defaultAdminDelayIncreaseWait());
- }
- function decreasingDelaySchedule(env e, uint48 newDelay) returns mathint {
- return e.block.timestamp + defaultAdminDelay(e) - newDelay;
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Invariant: defaultAdmin holds the DEFAULT_ADMIN_ROLE │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- invariant defaultAdminConsistency(address account)
- defaultAdmin() == account <=> hasRole(DEFAULT_ADMIN_ROLE(), account)
- {
- preserved {
- // defaultAdmin() returns the zero address when there's no default admin
- require nonZeroAccount(account);
- }
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Invariant: Only one account holds the DEFAULT_ADMIN_ROLE │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- invariant singleDefaultAdmin(address account, address another)
- hasRole(DEFAULT_ADMIN_ROLE(), account) && hasRole(DEFAULT_ADMIN_ROLE(), another) => another == account
- // We filter here because we couldn't find a way to force Certora to have an initial state with
- // only one DEFAULT_ADMIN_ROLE enforced, so a counter example is a different default admin since inception
- // triggering the transfer, which is known to be impossible by definition.
- filtered { f -> f.selector != acceptDefaultAdminTransfer().selector }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Invariant: DEFAULT_ADMIN_ROLE's admin is always DEFAULT_ADMIN_ROLE │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- invariant defaultAdminRoleAdminConsistency()
- getRoleAdmin(DEFAULT_ADMIN_ROLE()) == DEFAULT_ADMIN_ROLE()
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Invariant: owner is the defaultAdmin │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- invariant ownerConsistency()
- defaultAdmin() == owner()
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: revokeRole only affects the specified user/role combo │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule revokeRoleEffect(env e, bytes32 role) {
- require nonpayable(e);
- bytes32 otherRole;
- address account;
- address otherAccount;
- bool isCallerAdmin = hasRole(getRoleAdmin(role), e.msg.sender);
- bool hasOtherRoleBefore = hasRole(otherRole, otherAccount);
- revokeRole@withrevert(e, role, account);
- bool success = !lastReverted;
- bool hasOtherRoleAfter = hasRole(otherRole, otherAccount);
- // liveness
- assert success <=> isCallerAdmin && role != DEFAULT_ADMIN_ROLE(),
- "roles can only be revoked by their owner except for the default admin role";
- // effect
- assert success => !hasRole(role, account), "role is revoked";
- // no side effect
- assert hasOtherRoleBefore != hasOtherRoleAfter => (role == otherRole && account == otherAccount),
- "no other role is affected";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: renounceRole only affects the specified user/role combo │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule renounceRoleEffect(env e, bytes32 role) {
- require nonpayable(e);
- bytes32 otherRole;
- address account;
- address otherAccount;
- bool hasOtherRoleBefore = hasRole(otherRole, otherAccount);
- uint48 scheduleBefore = pendingDefaultAdminSchedule_();
- address pendingAdminBefore = pendingDefaultAdmin_();
- renounceRole@withrevert(e, role, account);
- bool success = !lastReverted;
- bool hasOtherRoleAfter = hasRole(otherRole, otherAccount);
- // liveness
- assert success <=> (
- account == e.msg.sender &&
- (
- (
- role != DEFAULT_ADMIN_ROLE()
- ) || (
- role == DEFAULT_ADMIN_ROLE() &&
- pendingAdminBefore == 0 &&
- isSet(scheduleBefore) &&
- hasPassed(e, scheduleBefore)
- )
- )
- ), "an account only can renounce by itself with a delay for the default admin role";
- // effect
- assert success => !hasRole(role, account), "role is renounced";
- // no side effect
- assert hasOtherRoleBefore != hasOtherRoleAfter => (role == otherRole && account == otherAccount),
- "no other role is affected";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: defaultAdmin is only affected by accepting an admin transfer or renoucing │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule noDefaultAdminChange(env e, method f, calldataarg args) {
- require nonZeroAccount(e.msg.sender);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- address adminBefore = defaultAdmin();
- f(e, args);
- address adminAfter = defaultAdmin();
- assert adminBefore != adminAfter => (
- f.selector == acceptDefaultAdminTransfer().selector ||
- f.selector == renounceRole(bytes32,address).selector
- ), "default admin is only affected by accepting an admin transfer or renoucing";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: pendingDefaultAdmin is only affected by beginning, accepting or canceling an admin transfer │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule noPendingDefaultAdminChange(env e, method f, calldataarg args) {
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- address pendingAdminBefore = pendingDefaultAdmin_();
- address scheduleBefore = pendingDefaultAdminSchedule_();
- f(e, args);
- address pendingAdminAfter = pendingDefaultAdmin_();
- address scheduleAfter = pendingDefaultAdminSchedule_();
- assert (
- pendingAdminBefore != pendingAdminAfter ||
- scheduleBefore != scheduleAfter
- ) => (
- f.selector == beginDefaultAdminTransfer(address).selector ||
- f.selector == acceptDefaultAdminTransfer().selector ||
- f.selector == cancelDefaultAdminTransfer().selector
- ), "pending admin and its schedule is only affected by beginning, accepting or cancelling an admin transfer";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: defaultAdminDelay can't be changed atomically by any function │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule noDefaultAdminDelayChange(env e, method f, calldataarg args) {
- uint48 delayBefore = defaultAdminDelay(e);
- f(e, args);
- uint48 delayAfter = defaultAdminDelay(e);
- assert delayBefore == delayAfter, "delay can't be changed atomically by any function";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: pendingDefaultAdminDelay is only affected by changeDefaultAdminDelay or rollbackDefaultAdminDelay │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule noPendingDefaultAdminDelayChange(env e, method f, calldataarg args) {
- uint48 pendingDelayBefore = pendingDelay_(e);
- f(e, args);
- uint48 pendingDelayAfter = pendingDelay_(e);
- assert pendingDelayBefore != pendingDelayAfter => (
- f.selector == changeDefaultAdminDelay(uint48).selector ||
- f.selector == rollbackDefaultAdminDelay().selector
- ), "pending delay is only affected by changeDefaultAdminDelay or rollbackDefaultAdminDelay";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: defaultAdminDelayIncreaseWait can't be changed atomically by any function │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule noDefaultAdminDelayIncreaseWaitChange(env e, method f, calldataarg args) {
- uint48 delayIncreaseWaitBefore = defaultAdminDelayIncreaseWait();
- f(e, args);
- uint48 delayIncreaseWaitAfter = defaultAdminDelayIncreaseWait();
- assert delayIncreaseWaitBefore == delayIncreaseWaitAfter,
- "delay increase wait can't be changed atomically by any function";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: beginDefaultAdminTransfer sets a pending default admin and its schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule beginDefaultAdminTransfer(env e, address newAdmin) {
- require nonpayable(e);
- require timeSanity(e);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- beginDefaultAdminTransfer@withrevert(e, newAdmin);
- bool success = !lastReverted;
- // liveness
- assert success <=> e.msg.sender == defaultAdmin(),
- "only the current default admin can begin a transfer";
- // effect
- assert success => pendingDefaultAdmin_() == newAdmin,
- "pending default admin is set";
- assert success => pendingDefaultAdminSchedule_() == e.block.timestamp + defaultAdminDelay(e),
- "pending default admin delay is set";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: A default admin can't change in less than the applied schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule pendingDefaultAdminDelayEnforced(env e1, env e2, method f, calldataarg args, address newAdmin) {
- require e1.block.timestamp < e2.block.timestamp;
- uint48 delayBefore = defaultAdminDelay(e1);
- address adminBefore = defaultAdmin();
- // There might be a better way to generalize this without requiring `beginDefaultAdminTransfer`, but currently
- // it's the only way in which we can attest that only `delayBefore` has passed before a change.
- beginDefaultAdminTransfer(e1, newAdmin);
- f(e2, args);
- address adminAfter = defaultAdmin();
- assert adminAfter == newAdmin => ((e2.block.timestamp >= e1.block.timestamp + delayBefore) || adminBefore == newAdmin),
- "A delay can't change in less than applied schedule";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: acceptDefaultAdminTransfer updates defaultAdmin resetting the pending admin and its schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule acceptDefaultAdminTransfer(env e) {
- require nonpayable(e);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- address pendingAdminBefore = pendingDefaultAdmin_();
- uint48 scheduleAfter = pendingDefaultAdminSchedule_();
- acceptDefaultAdminTransfer@withrevert(e);
- bool success = !lastReverted;
- // liveness
- assert success <=> e.msg.sender == pendingAdminBefore && isSet(scheduleAfter) && hasPassed(e, scheduleAfter),
- "only the pending default admin can accept the role after the schedule has been set and passed";
- // effect
- assert success => defaultAdmin() == pendingAdminBefore,
- "Default admin is set to the previous pending default admin";
- assert success => pendingDefaultAdmin_() == 0,
- "Pending default admin is reset";
- assert success => pendingDefaultAdminSchedule_() == 0,
- "Pending default admin delay is reset";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: cancelDefaultAdminTransfer resets pending default admin and its schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule cancelDefaultAdminTransfer(env e) {
- require nonpayable(e);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- cancelDefaultAdminTransfer@withrevert(e);
- bool success = !lastReverted;
- // liveness
- assert success <=> e.msg.sender == defaultAdmin(),
- "only the current default admin can cancel a transfer";
- // effect
- assert success => pendingDefaultAdmin_() == 0,
- "Pending default admin is reset";
- assert success => pendingDefaultAdminSchedule_() == 0,
- "Pending default admin delay is reset";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: changeDefaultAdminDelay sets a pending default admin delay and its schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule changeDefaultAdminDelay(env e, uint48 newDelay) {
- require nonpayable(e);
- require timeSanity(e);
- require delayChangeWaitSanity(e, newDelay);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- uint48 delayBefore = defaultAdminDelay(e);
- changeDefaultAdminDelay@withrevert(e, newDelay);
- bool success = !lastReverted;
- // liveness
- assert success <=> e.msg.sender == defaultAdmin(),
- "only the current default admin can begin a delay change";
- // effect
- assert success => pendingDelay_(e) == newDelay, "pending delay is set";
- assert success => (
- pendingDelaySchedule_(e) > e.block.timestamp ||
- delayBefore == newDelay || // Interpreted as decreasing, x - x = 0
- defaultAdminDelayIncreaseWait() == 0
- ),
- "pending delay schedule is set in the future unless accepted edge cases";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: A delay can't change in less than the applied schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule pendingDelayWaitEnforced(env e1, env e2, method f, calldataarg args, uint48 newDelay) {
- require e1.block.timestamp < e2.block.timestamp;
- uint48 delayBefore = defaultAdminDelay(e1);
- changeDefaultAdminDelay(e1, newDelay);
- f(e2, args);
- uint48 delayAfter = defaultAdminDelay(e2);
- mathint delayWait = newDelay > delayBefore ? increasingDelaySchedule(e1, newDelay) : decreasingDelaySchedule(e1, newDelay);
- assert delayAfter == newDelay => (e2.block.timestamp >= delayWait || delayBefore == newDelay),
- "A delay can't change in less than applied schedule";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: pending delay wait is set depending on increasing or decreasing the delay │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule pendingDelayWait(env e, uint48 newDelay) {
- uint48 oldDelay = defaultAdminDelay(e);
- changeDefaultAdminDelay(e, newDelay);
- assert newDelay > oldDelay => pendingDelaySchedule_(e) == increasingDelaySchedule(e, newDelay),
- "Delay wait is the minimum between the new delay and a threshold when the delay is increased";
- assert newDelay <= oldDelay => pendingDelaySchedule_(e) == decreasingDelaySchedule(e, newDelay),
- "Delay wait is the difference between the current and the new delay when the delay is decreased";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Function correctness: rollbackDefaultAdminDelay resets the delay and its schedule │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule rollbackDefaultAdminDelay(env e) {
- require nonpayable(e);
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- rollbackDefaultAdminDelay@withrevert(e);
- bool success = !lastReverted;
- // liveness
- assert success <=> e.msg.sender == defaultAdmin(),
- "only the current default admin can rollback a delay change";
- // effect
- assert success => pendingDelay_(e) == 0,
- "Pending default admin is reset";
- assert success => pendingDelaySchedule_(e) == 0,
- "Pending default admin delay is reset";
- }
- /*
- ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │ Rule: pending default admin and the delay can only change along with their corresponding schedules │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- */
- rule pendingValueAndScheduleCoupling(env e, address newAdmin, uint48 newDelay) {
- requireInvariant defaultAdminConsistency(defaultAdmin());
- requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
- // Pending admin
- address pendingAdminBefore = pendingDefaultAdmin_();
- uint48 pendingAdminScheduleBefore = pendingDefaultAdminSchedule_();
- beginDefaultAdminTransfer(e, newAdmin);
- address pendingAdminAfter = pendingDefaultAdmin_();
- uint48 pendingAdminScheduleAfter = pendingDefaultAdminSchedule_();
- assert (
- pendingAdminScheduleBefore != pendingDefaultAdminSchedule_() &&
- pendingAdminBefore == pendingAdminAfter
- ) => newAdmin == pendingAdminBefore, "pending admin stays the same if the new admin set is the same";
- assert (
- pendingAdminBefore != pendingAdminAfter &&
- pendingAdminScheduleBefore == pendingDefaultAdminSchedule_()
- ) => (
- // Schedule doesn't change if:
- // - The defaultAdminDelay was reduced to a value such that added to the block.timestamp is equal to previous schedule
- e.block.timestamp + defaultAdminDelay(e) == pendingAdminScheduleBefore
- ), "pending admin stays the same if a default admin transfer is begun on accepted edge cases";
- // Pending delay
- address pendingDelayBefore = pendingDelay_(e);
- uint48 pendingDelayScheduleBefore = pendingDelaySchedule_(e);
- changeDefaultAdminDelay(e, newDelay);
- address pendingDelayAfter = pendingDelay_(e);
- uint48 pendingDelayScheduleAfter = pendingDelaySchedule_(e);
- assert (
- pendingDelayScheduleBefore != pendingDelayScheduleAfter &&
- pendingDelayBefore == pendingDelayAfter
- ) => newDelay == pendingDelayBefore || pendingDelayBefore == 0, "pending delay stays the same if the new delay set is the same";
- assert (
- pendingDelayBefore != pendingDelayAfter &&
- pendingDelayScheduleBefore == pendingDelayScheduleAfter
- ) => (
- increasingDelaySchedule(e, newDelay) == pendingDelayScheduleBefore ||
- decreasingDelaySchedule(e, newDelay) == pendingDelayScheduleBefore
- ), "pending delay stays the same if a default admin transfer is begun on accepted edge cases";
- }
|