AccessControlDefaultAdminRules.spec 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import "helpers/helpers.spec"
  2. import "methods/IAccessControlDefaultAdminRules.spec"
  3. import "methods/IAccessControl.spec"
  4. import "AccessControl.spec"
  5. use rule onlyGrantCanGrant filtered {
  6. f -> f.selector != acceptDefaultAdminTransfer().selector
  7. }
  8. /*
  9. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  10. │ Helpers │
  11. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  12. */
  13. function max_uint48() returns mathint {
  14. return (1 << 48) - 1;
  15. }
  16. function nonZeroAccount(address account) returns bool {
  17. return account != 0;
  18. }
  19. function timeSanity(env e) returns bool {
  20. return
  21. e.block.timestamp > 0 && // Avoids 0 schedules
  22. e.block.timestamp + defaultAdminDelay(e) < max_uint48();
  23. }
  24. function delayChangeWaitSanity(env e, uint48 newDelay) returns bool {
  25. return e.block.timestamp + delayChangeWait_(e, newDelay) < max_uint48();
  26. }
  27. function isSet(uint48 schedule) returns bool {
  28. return schedule != 0;
  29. }
  30. function hasPassed(env e, uint48 schedule) returns bool {
  31. return schedule < e.block.timestamp;
  32. }
  33. function min(uint48 a, uint48 b) returns mathint {
  34. return a < b ? a : b;
  35. }
  36. function increasingDelaySchedule(env e, uint48 newDelay) returns mathint {
  37. return e.block.timestamp + min(newDelay, defaultAdminDelayIncreaseWait());
  38. }
  39. function decreasingDelaySchedule(env e, uint48 newDelay) returns mathint {
  40. return e.block.timestamp + defaultAdminDelay(e) - newDelay;
  41. }
  42. /*
  43. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  44. │ Invariant: defaultAdmin holds the DEFAULT_ADMIN_ROLE │
  45. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  46. */
  47. invariant defaultAdminConsistency(address account)
  48. defaultAdmin() == account <=> hasRole(DEFAULT_ADMIN_ROLE(), account)
  49. {
  50. preserved {
  51. // defaultAdmin() returns the zero address when there's no default admin
  52. require nonZeroAccount(account);
  53. }
  54. }
  55. /*
  56. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  57. │ Invariant: Only one account holds the DEFAULT_ADMIN_ROLE │
  58. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  59. */
  60. invariant singleDefaultAdmin(address account, address another)
  61. hasRole(DEFAULT_ADMIN_ROLE(), account) && hasRole(DEFAULT_ADMIN_ROLE(), another) => another == account
  62. // We filter here because we couldn't find a way to force Certora to have an initial state with
  63. // only one DEFAULT_ADMIN_ROLE enforced, so a counter example is a different default admin since inception
  64. // triggering the transfer, which is known to be impossible by definition.
  65. filtered { f -> f.selector != acceptDefaultAdminTransfer().selector }
  66. /*
  67. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  68. │ Invariant: DEFAULT_ADMIN_ROLE's admin is always DEFAULT_ADMIN_ROLE │
  69. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  70. */
  71. invariant defaultAdminRoleAdminConsistency()
  72. getRoleAdmin(DEFAULT_ADMIN_ROLE()) == DEFAULT_ADMIN_ROLE()
  73. /*
  74. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  75. │ Invariant: owner is the defaultAdmin │
  76. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  77. */
  78. invariant ownerConsistency()
  79. defaultAdmin() == owner()
  80. /*
  81. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  82. │ Function correctness: revokeRole only affects the specified user/role combo │
  83. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  84. */
  85. rule revokeRoleEffect(env e, bytes32 role) {
  86. require nonpayable(e);
  87. bytes32 otherRole;
  88. address account;
  89. address otherAccount;
  90. bool isCallerAdmin = hasRole(getRoleAdmin(role), e.msg.sender);
  91. bool hasOtherRoleBefore = hasRole(otherRole, otherAccount);
  92. revokeRole@withrevert(e, role, account);
  93. bool success = !lastReverted;
  94. bool hasOtherRoleAfter = hasRole(otherRole, otherAccount);
  95. // liveness
  96. assert success <=> isCallerAdmin && role != DEFAULT_ADMIN_ROLE(),
  97. "roles can only be revoked by their owner except for the default admin role";
  98. // effect
  99. assert success => !hasRole(role, account), "role is revoked";
  100. // no side effect
  101. assert hasOtherRoleBefore != hasOtherRoleAfter => (role == otherRole && account == otherAccount),
  102. "no other role is affected";
  103. }
  104. /*
  105. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  106. │ Function correctness: renounceRole only affects the specified user/role combo │
  107. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  108. */
  109. rule renounceRoleEffect(env e, bytes32 role) {
  110. require nonpayable(e);
  111. bytes32 otherRole;
  112. address account;
  113. address otherAccount;
  114. bool hasOtherRoleBefore = hasRole(otherRole, otherAccount);
  115. uint48 scheduleBefore = pendingDefaultAdminSchedule_();
  116. address pendingAdminBefore = pendingDefaultAdmin_();
  117. renounceRole@withrevert(e, role, account);
  118. bool success = !lastReverted;
  119. bool hasOtherRoleAfter = hasRole(otherRole, otherAccount);
  120. // liveness
  121. assert success <=> (
  122. account == e.msg.sender &&
  123. (
  124. (
  125. role != DEFAULT_ADMIN_ROLE()
  126. ) || (
  127. role == DEFAULT_ADMIN_ROLE() &&
  128. pendingAdminBefore == 0 &&
  129. isSet(scheduleBefore) &&
  130. hasPassed(e, scheduleBefore)
  131. )
  132. )
  133. ), "an account only can renounce by itself with a delay for the default admin role";
  134. // effect
  135. assert success => !hasRole(role, account), "role is renounced";
  136. // no side effect
  137. assert hasOtherRoleBefore != hasOtherRoleAfter => (role == otherRole && account == otherAccount),
  138. "no other role is affected";
  139. }
  140. /*
  141. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  142. │ Rule: defaultAdmin is only affected by accepting an admin transfer or renoucing │
  143. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  144. */
  145. rule noDefaultAdminChange(env e, method f, calldataarg args) {
  146. require nonZeroAccount(e.msg.sender);
  147. requireInvariant defaultAdminConsistency(defaultAdmin());
  148. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  149. address adminBefore = defaultAdmin();
  150. f(e, args);
  151. address adminAfter = defaultAdmin();
  152. assert adminBefore != adminAfter => (
  153. f.selector == acceptDefaultAdminTransfer().selector ||
  154. f.selector == renounceRole(bytes32,address).selector
  155. ), "default admin is only affected by accepting an admin transfer or renoucing";
  156. }
  157. /*
  158. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  159. │ Rule: pendingDefaultAdmin is only affected by beginning, accepting or canceling an admin transfer │
  160. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  161. */
  162. rule noPendingDefaultAdminChange(env e, method f, calldataarg args) {
  163. requireInvariant defaultAdminConsistency(defaultAdmin());
  164. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  165. address pendingAdminBefore = pendingDefaultAdmin_();
  166. address scheduleBefore = pendingDefaultAdminSchedule_();
  167. f(e, args);
  168. address pendingAdminAfter = pendingDefaultAdmin_();
  169. address scheduleAfter = pendingDefaultAdminSchedule_();
  170. assert (
  171. pendingAdminBefore != pendingAdminAfter ||
  172. scheduleBefore != scheduleAfter
  173. ) => (
  174. f.selector == beginDefaultAdminTransfer(address).selector ||
  175. f.selector == acceptDefaultAdminTransfer().selector ||
  176. f.selector == cancelDefaultAdminTransfer().selector
  177. ), "pending admin and its schedule is only affected by beginning, accepting or cancelling an admin transfer";
  178. }
  179. /*
  180. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  181. │ Rule: defaultAdminDelay can't be changed atomically by any function │
  182. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  183. */
  184. rule noDefaultAdminDelayChange(env e, method f, calldataarg args) {
  185. uint48 delayBefore = defaultAdminDelay(e);
  186. f(e, args);
  187. uint48 delayAfter = defaultAdminDelay(e);
  188. assert delayBefore == delayAfter, "delay can't be changed atomically by any function";
  189. }
  190. /*
  191. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  192. │ Rule: pendingDefaultAdminDelay is only affected by changeDefaultAdminDelay or rollbackDefaultAdminDelay │
  193. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  194. */
  195. rule noPendingDefaultAdminDelayChange(env e, method f, calldataarg args) {
  196. uint48 pendingDelayBefore = pendingDelay_(e);
  197. f(e, args);
  198. uint48 pendingDelayAfter = pendingDelay_(e);
  199. assert pendingDelayBefore != pendingDelayAfter => (
  200. f.selector == changeDefaultAdminDelay(uint48).selector ||
  201. f.selector == rollbackDefaultAdminDelay().selector
  202. ), "pending delay is only affected by changeDefaultAdminDelay or rollbackDefaultAdminDelay";
  203. }
  204. /*
  205. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  206. │ Rule: defaultAdminDelayIncreaseWait can't be changed atomically by any function │
  207. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  208. */
  209. rule noDefaultAdminDelayIncreaseWaitChange(env e, method f, calldataarg args) {
  210. uint48 delayIncreaseWaitBefore = defaultAdminDelayIncreaseWait();
  211. f(e, args);
  212. uint48 delayIncreaseWaitAfter = defaultAdminDelayIncreaseWait();
  213. assert delayIncreaseWaitBefore == delayIncreaseWaitAfter,
  214. "delay increase wait can't be changed atomically by any function";
  215. }
  216. /*
  217. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  218. │ Function correctness: beginDefaultAdminTransfer sets a pending default admin and its schedule │
  219. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  220. */
  221. rule beginDefaultAdminTransfer(env e, address newAdmin) {
  222. require nonpayable(e);
  223. require timeSanity(e);
  224. requireInvariant defaultAdminConsistency(defaultAdmin());
  225. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  226. beginDefaultAdminTransfer@withrevert(e, newAdmin);
  227. bool success = !lastReverted;
  228. // liveness
  229. assert success <=> e.msg.sender == defaultAdmin(),
  230. "only the current default admin can begin a transfer";
  231. // effect
  232. assert success => pendingDefaultAdmin_() == newAdmin,
  233. "pending default admin is set";
  234. assert success => pendingDefaultAdminSchedule_() == e.block.timestamp + defaultAdminDelay(e),
  235. "pending default admin delay is set";
  236. }
  237. /*
  238. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  239. │ Rule: A default admin can't change in less than the applied schedule │
  240. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  241. */
  242. rule pendingDefaultAdminDelayEnforced(env e1, env e2, method f, calldataarg args, address newAdmin) {
  243. require e1.block.timestamp < e2.block.timestamp;
  244. uint48 delayBefore = defaultAdminDelay(e1);
  245. address adminBefore = defaultAdmin();
  246. // There might be a better way to generalize this without requiring `beginDefaultAdminTransfer`, but currently
  247. // it's the only way in which we can attest that only `delayBefore` has passed before a change.
  248. beginDefaultAdminTransfer(e1, newAdmin);
  249. f(e2, args);
  250. address adminAfter = defaultAdmin();
  251. assert adminAfter == newAdmin => ((e2.block.timestamp >= e1.block.timestamp + delayBefore) || adminBefore == newAdmin),
  252. "A delay can't change in less than applied schedule";
  253. }
  254. /*
  255. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  256. │ Function correctness: acceptDefaultAdminTransfer updates defaultAdmin resetting the pending admin and its schedule │
  257. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  258. */
  259. rule acceptDefaultAdminTransfer(env e) {
  260. require nonpayable(e);
  261. requireInvariant defaultAdminConsistency(defaultAdmin());
  262. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  263. address pendingAdminBefore = pendingDefaultAdmin_();
  264. uint48 scheduleAfter = pendingDefaultAdminSchedule_();
  265. acceptDefaultAdminTransfer@withrevert(e);
  266. bool success = !lastReverted;
  267. // liveness
  268. assert success <=> e.msg.sender == pendingAdminBefore && isSet(scheduleAfter) && hasPassed(e, scheduleAfter),
  269. "only the pending default admin can accept the role after the schedule has been set and passed";
  270. // effect
  271. assert success => defaultAdmin() == pendingAdminBefore,
  272. "Default admin is set to the previous pending default admin";
  273. assert success => pendingDefaultAdmin_() == 0,
  274. "Pending default admin is reset";
  275. assert success => pendingDefaultAdminSchedule_() == 0,
  276. "Pending default admin delay is reset";
  277. }
  278. /*
  279. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  280. │ Function correctness: cancelDefaultAdminTransfer resets pending default admin and its schedule │
  281. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  282. */
  283. rule cancelDefaultAdminTransfer(env e) {
  284. require nonpayable(e);
  285. requireInvariant defaultAdminConsistency(defaultAdmin());
  286. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  287. cancelDefaultAdminTransfer@withrevert(e);
  288. bool success = !lastReverted;
  289. // liveness
  290. assert success <=> e.msg.sender == defaultAdmin(),
  291. "only the current default admin can cancel a transfer";
  292. // effect
  293. assert success => pendingDefaultAdmin_() == 0,
  294. "Pending default admin is reset";
  295. assert success => pendingDefaultAdminSchedule_() == 0,
  296. "Pending default admin delay is reset";
  297. }
  298. /*
  299. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  300. │ Function correctness: changeDefaultAdminDelay sets a pending default admin delay and its schedule │
  301. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  302. */
  303. rule changeDefaultAdminDelay(env e, uint48 newDelay) {
  304. require nonpayable(e);
  305. require timeSanity(e);
  306. require delayChangeWaitSanity(e, newDelay);
  307. requireInvariant defaultAdminConsistency(defaultAdmin());
  308. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  309. uint48 delayBefore = defaultAdminDelay(e);
  310. changeDefaultAdminDelay@withrevert(e, newDelay);
  311. bool success = !lastReverted;
  312. // liveness
  313. assert success <=> e.msg.sender == defaultAdmin(),
  314. "only the current default admin can begin a delay change";
  315. // effect
  316. assert success => pendingDelay_(e) == newDelay, "pending delay is set";
  317. assert success => (
  318. pendingDelaySchedule_(e) > e.block.timestamp ||
  319. delayBefore == newDelay || // Interpreted as decreasing, x - x = 0
  320. defaultAdminDelayIncreaseWait() == 0
  321. ),
  322. "pending delay schedule is set in the future unless accepted edge cases";
  323. }
  324. /*
  325. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  326. │ Rule: A delay can't change in less than the applied schedule │
  327. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  328. */
  329. rule pendingDelayWaitEnforced(env e1, env e2, method f, calldataarg args, uint48 newDelay) {
  330. require e1.block.timestamp < e2.block.timestamp;
  331. uint48 delayBefore = defaultAdminDelay(e1);
  332. changeDefaultAdminDelay(e1, newDelay);
  333. f(e2, args);
  334. uint48 delayAfter = defaultAdminDelay(e2);
  335. mathint delayWait = newDelay > delayBefore ? increasingDelaySchedule(e1, newDelay) : decreasingDelaySchedule(e1, newDelay);
  336. assert delayAfter == newDelay => (e2.block.timestamp >= delayWait || delayBefore == newDelay),
  337. "A delay can't change in less than applied schedule";
  338. }
  339. /*
  340. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  341. │ Rule: pending delay wait is set depending on increasing or decreasing the delay │
  342. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  343. */
  344. rule pendingDelayWait(env e, uint48 newDelay) {
  345. uint48 oldDelay = defaultAdminDelay(e);
  346. changeDefaultAdminDelay(e, newDelay);
  347. assert newDelay > oldDelay => pendingDelaySchedule_(e) == increasingDelaySchedule(e, newDelay),
  348. "Delay wait is the minimum between the new delay and a threshold when the delay is increased";
  349. assert newDelay <= oldDelay => pendingDelaySchedule_(e) == decreasingDelaySchedule(e, newDelay),
  350. "Delay wait is the difference between the current and the new delay when the delay is decreased";
  351. }
  352. /*
  353. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  354. │ Function correctness: rollbackDefaultAdminDelay resets the delay and its schedule │
  355. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  356. */
  357. rule rollbackDefaultAdminDelay(env e) {
  358. require nonpayable(e);
  359. requireInvariant defaultAdminConsistency(defaultAdmin());
  360. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  361. rollbackDefaultAdminDelay@withrevert(e);
  362. bool success = !lastReverted;
  363. // liveness
  364. assert success <=> e.msg.sender == defaultAdmin(),
  365. "only the current default admin can rollback a delay change";
  366. // effect
  367. assert success => pendingDelay_(e) == 0,
  368. "Pending default admin is reset";
  369. assert success => pendingDelaySchedule_(e) == 0,
  370. "Pending default admin delay is reset";
  371. }
  372. /*
  373. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  374. │ Rule: pending default admin and the delay can only change along with their corresponding schedules │
  375. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  376. */
  377. rule pendingValueAndScheduleCoupling(env e, address newAdmin, uint48 newDelay) {
  378. requireInvariant defaultAdminConsistency(defaultAdmin());
  379. requireInvariant singleDefaultAdmin(e.msg.sender, defaultAdmin());
  380. // Pending admin
  381. address pendingAdminBefore = pendingDefaultAdmin_();
  382. uint48 pendingAdminScheduleBefore = pendingDefaultAdminSchedule_();
  383. beginDefaultAdminTransfer(e, newAdmin);
  384. address pendingAdminAfter = pendingDefaultAdmin_();
  385. uint48 pendingAdminScheduleAfter = pendingDefaultAdminSchedule_();
  386. assert (
  387. pendingAdminScheduleBefore != pendingDefaultAdminSchedule_() &&
  388. pendingAdminBefore == pendingAdminAfter
  389. ) => newAdmin == pendingAdminBefore, "pending admin stays the same if the new admin set is the same";
  390. assert (
  391. pendingAdminBefore != pendingAdminAfter &&
  392. pendingAdminScheduleBefore == pendingDefaultAdminSchedule_()
  393. ) => (
  394. // Schedule doesn't change if:
  395. // - The defaultAdminDelay was reduced to a value such that added to the block.timestamp is equal to previous schedule
  396. e.block.timestamp + defaultAdminDelay(e) == pendingAdminScheduleBefore
  397. ), "pending admin stays the same if a default admin transfer is begun on accepted edge cases";
  398. // Pending delay
  399. address pendingDelayBefore = pendingDelay_(e);
  400. uint48 pendingDelayScheduleBefore = pendingDelaySchedule_(e);
  401. changeDefaultAdminDelay(e, newDelay);
  402. address pendingDelayAfter = pendingDelay_(e);
  403. uint48 pendingDelayScheduleAfter = pendingDelaySchedule_(e);
  404. assert (
  405. pendingDelayScheduleBefore != pendingDelayScheduleAfter &&
  406. pendingDelayBefore == pendingDelayAfter
  407. ) => newDelay == pendingDelayBefore || pendingDelayBefore == 0, "pending delay stays the same if the new delay set is the same";
  408. assert (
  409. pendingDelayBefore != pendingDelayAfter &&
  410. pendingDelayScheduleBefore == pendingDelayScheduleAfter
  411. ) => (
  412. increasingDelaySchedule(e, newDelay) == pendingDelayScheduleBefore ||
  413. decreasingDelaySchedule(e, newDelay) == pendingDelayScheduleBefore
  414. ), "pending delay stays the same if a default admin transfer is begun on accepted edge cases";
  415. }