Security Audit
Jan 9th, 2024
Version 1.0.0
Presented by 0xMacro
This document includes the results of the security audit for Mento's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from October 17, 2023 to October 20, 2023.
The purpose of this audit is to review the source code of certain Mento Solidity contracts, and provide feedback on the design, architecture, and quality of the source code with an emphasis on validating the correctness and security of the software in its entirety.
Disclaimer: While Macro’s review is comprehensive and has surfaced some changes that should be made to the source code, this audit should not solely be relied upon for security, as no single audit is guaranteed to catch all possible bugs.
The following is an aggregation of issues found by the Macro Audit team:
Severity | Count | Acknowledged | Won't Do | Addressed |
---|---|---|---|---|
Low | 3 | 1 | - | 2 |
Code Quality | 7 | 1 | 2 | 4 |
Gas Optimization | 1 | 1 | - | - |
Mento was quick to respond to these issues.
Our understanding of the specification was based on the following sources:
The following source code was reviewed during the audit:
0ea8149579ab277d181f342ff218bb2750eec04c
61eb278c1a1a16f103f6f1cfbc5b27aa577732f7
Specifically, we audited the following contracts within this repository:
Source Code | SHA256 |
---|---|
./contracts/governance/Airgrab.sol |
|
./contracts/governance/Emission.sol |
|
./contracts/governance/Factory.sol |
|
./contracts/governance/MentoGovernor.sol |
|
./contracts/governance/MentoToken.sol |
|
./contracts/governance/TimelockController.sol |
|
Additionally, the following forked contracts were reviewed for specific Celo changes but not fully audited as they a direct for from Raribles Locking contracts:
Source Code | SHA256 |
---|---|
./contracts/governance/locking/Locking.sol |
|
./contracts/governance/locking/LockingBase.sol |
|
./contracts/governance/locking/LockingRelock.sol |
|
./contracts/governance/locking/LockingVotes.sol |
|
./contracts/governance/locking/interfaces/ILocking.sol |
|
./contracts/governance/locking/interfaces/INextVersionLock.sol |
|
./contracts/governance/locking/libs/LibBrokenLine.sol |
|
./contracts/governance/locking/libs/LibIntMapping.sol |
|
Note: This document contains an audit solely of the Solidity contracts listed above. Specifically, the audit pertains only to the contracts themselves, and does not pertain to any other programs or scripts, including deployment scripts.
Click on an issue to jump to it, or scroll down to see them all.
Locking
contract can underflow
address(0)
checks
uint256
cast
treasury
address can be set in the constructor
ERC20
inheritance
Airgrab
We quantify issues in three parts:
This third part – the severity level – is a summary of how much consideration the client should give to fixing the issue. We assign severity according to the table of guidelines below:
Severity | Description |
---|---|
(C-x) Critical |
We recommend the client must fix the issue, no matter what, because not fixing would mean significant funds/assets WILL be lost. |
(H-x) High |
We recommend the client must address the issue, no matter what, because not fixing would be very bad, or some funds/assets will be lost, or the code’s behavior is against the provided spec. |
(M-x) Medium |
We recommend the client to seriously consider fixing the issue, as the implications of not fixing the issue are severe enough to impact the project significantly, albiet not in an existential manner. |
(L-x) Low |
The risk is small, unlikely, or may not relevant to the project in a meaningful way. Whether or not the project wants to develop a fix is up to the goals and needs of the project. |
(Q-x) Code Quality |
The issue identified does not pose any obvious risk, but fixing could improve overall code quality, on-chain composability, developer ergonomics, or even certain aspects of protocol design. |
(I-x) Informational |
Warnings and things to keep in mind when operating the protocol. No immediate action required. |
(G-x) Gas Optimizations |
The presented optimization suggestion would save an amount of gas significant enough, in our opinion, to be worth the development cost of implementing it. |
Reference: Emission.sol#L46-49
The Emission
contract includes an onlyOwner
setter function for the token. If this token is changed mid-cycle, the emission schedule will be disrupted, counting previously emitted tokens as emitted for the new token and potentially bricking the programmed amounts of governance token supply for any given time.
Remediations to Consider
Consider allowing this function to only set the token contract once.
Contracts like MentoToken.sol
, Emission.sol
, and Airgrab.sol
have initializers and/or setters that are used to set values after deployment. This is due to circular dependencies where they each need the address of another contract that may yet be deployed in the factory contract’s createGovernance()
call. However, there is a way to know the address of a contract before it is deployed, since when deploying a contract using the CREATE
opcode, it will deterministically deploy the contract with an address based on the contract’s nonce value. Since createGoverance()
will only be called once, these values are known and the addresses of the contracts to be deployed can be predicted in advance and used in the constructors of other contracts before their deployment, provided no calls are made to them before they are deployed.
Remediations to Consider
Remove unnecessary setters and initializers by predicting the required addresses before they are deployed. This will reduce the bytecode size of the contracts as well as remove the perception that these contracts are upgradable/mutable.
Locking
contract can underflow
In LockingBase.sol
’s getLock()
function, a call is made to divUp()
to get the lockSlope
. The lockAmount
is passed in as the a
parameter which gets subtracted by 1 before it is divided.
function divUp(uint96 a, uint96 b) internal pure returns (uint96) {
return ((a - 1) / b) + 1;
}
Reference: LockingBase#L219-L221
If lockSlope
is 0, then the function will revert with an underflow. The case where the lockSlope
can be set to zero is if the amountMultiplied
is less than the ST_FORMULA_DIVIDER
, which can occur if the cliff
is set to zero, the slopePeriod
is equal to the minSlopePeriod
, which is 1, and the amount is sufficiently small, ≤ 5.
function getLock(
uint96 amount,
uint32 slopePeriod,
uint32 cliff
) public view returns (uint96 lockAmount, uint96 lockSlope) {
require(cliff >= minCliffPeriod, "cliff period < minimal lock period");
require(slopePeriod >= minSlopePeriod, "slope period < minimal lock period");
uint96 cliffSide = (uint96(cliff - uint32(minCliffPeriod)) * (ST_FORMULA_CLIFF_MULTIPLIER)) /
(MAX_CLIFF_PERIOD - uint32(minCliffPeriod));
uint96 slopeSide = (uint96((slopePeriod - uint32(minSlopePeriod))) * (ST_FORMULA_SLOPE_MULTIPLIER)) /
(MAX_SLOPE_PERIOD - uint32(minSlopePeriod));
uint96 multiplier = cliffSide + (slopeSide) + (ST_FORMULA_CONST_MULTIPLIER);
uint256 amountMultiplied = uint256(amount) * uint256(multiplier);
lockAmount = uint96(amountMultiplied / (ST_FORMULA_DIVIDER));
lockSlope = divUp(lockAmount, slopePeriod);
}
Reference: LockingBase#L200-L221
Remediations to Consider
This case should be handled by a custom error, or the minimum amount to lock should be sufficiently high to prevent this underflow from occurring.
We are not going to fix this because:
1- Probability of this happening is really small, only happens for really low lock amounts
2- Impact is low, ie a wrong revert message
3- We want to keep the changes to the forked library minimal
address(0)
checks
References: Emission.sol#L46-49, Emission.sol#L55-58, LockingBase.sol#L148, MentoToken.sol#L31-53
Consider adding sanity checks for all address setters to avoid misconfigurations with address(0)
as a value.
We think address(0) check does not provide value in our case. We believe address(0) is not that different than address(1) when the function is called by the governance.
uint256
cast
Reference: Airgrab.sol#L131
In the Airgrab.sol
contract, the modifier canClaim()
unnecessarily casts the input parameter amount
to uint256
, even though it is already a uint256
variable. Consider removing this unnecessary typecast.
Reference: Airgrab.sol#L28-29, LockingBase.sol#L18-19
Both Airgrab
and LockingBase
contracts declare constant variables MAX_CLIFF_PERIOD
and MAX_SLOPE_PERIOD
. Consider moving these declarations to the shared imported interface ILocking.sol
to keep one source of truth for these values.
ILocking is not shared between LockingBase and Airgrab. Fixing this would require us to introduce a shared interface and we are not sure if it worths it
treasury
address can be set in the constructor
Reference: Airgrab.sol#L66, LockingBase.sol#L190
The treasury
variable, in Airgrab
, is set in the initialize()
function, used as a workaround for the circular dependencies with the Token
and LockingContract
. However, this value does not need to be initialized after deployment. Consider making it immutable and setting it in the constructor instead.
There are some comments that have spelling errors:
* @dev Check if the account can cliam
Reference: Airgrab.sol#L116
* if the account hasn't already claimed and if it's incldued
Reference: Airgrab.sol#L118
* @notice This contract implements a token airgrab gated by a MerkeTree and KYC using fractal.
Reference: Airgrab.sol#L18
uint256 scheduledRemainigSupply = (TOTAL_EMISSION_SUPPLY * (positiveAggregate - negativeAggregate)) / SCALER;
Reference: Emission.sol#L100
Throughout the codebase, require statements are used, which revert with a set static string. For solidity versions 0.8.4+, custom errors can be used, which can return dynamic values to give more insight into what caused the error, as well as cost less gas on average.
Remediations to Consider
Replace all require statements with conditions that revert with a custom error containing relevant error information, in order to make it more clear why an error is occurring to the user as well as save slightly on gas costs.
We use require statements across whole code base and want to keep it consistent.
ERC20
inheritance
MentoToken.sol
inherits from both ERC20
, and ERC20Burnable
, however, ERC20Burnable
also inherits ERC20.
contract MentoToken is ERC20, ERC20Burnable {
Reference: MentoToken.sol#L12
abstract contract ERC20Burnable is Context, ERC20 {
Reference: ERC20Burnable.sol#L14
Remediations to Consider
Remove the ERC20
dependency and only inherit from ERC20Burnable
to prevent duplicate inheritance.
Airgrab
In Airgrab.sol
, there are reentrancy guards on the functions claim()
and drain()
. However, there is no path for a reentrancy attack to occur. claim()
properly follows the checks effects and interactions pattern by setting claimed
to true for the sender before making an external call to the locking contract. Since the canClaim
modifier is run and requires claimed
for the sender to be false, there is no way to claim multiple times, even if the locking contract was malicious.
For drain()
, there would be no benefit to reenter the call as it is just transferring the contract’s balance of tokens to the treasury contract, and no intermediate state can be manipulated.
Remediations to Consider
Remove the reentrancy guards on claim()
and drain()
and remove it as a dependency, to reduce the gas costs of calling these functions and reduce the deployment cost of the contract.
We agree that it looks unnecessary, but we still want to keep it as it is a function that will be used just for once per user and extra gas spent will be negligible, especially on Celo.
Macro makes no warranties, either express, implied, statutory, or otherwise, with respect to the services or deliverables provided in this report, and Macro specifically disclaims all implied warranties of merchantability, fitness for a particular purpose, noninfringement and those arising from a course of dealing, usage or trade with respect thereto, and all such warranties are hereby excluded to the fullest extent permitted by law.
Macro will not be liable for any lost profits, business, contracts, revenue, goodwill, production, anticipated savings, loss of data, or costs of procurement of substitute goods or services or for any claim or demand by any other party. In no event will Macro be liable for consequential, incidental, special, indirect, or exemplary damages arising out of this agreement or any work statement, however caused and (to the fullest extent permitted by law) under any theory of liability (including negligence), even if Macro has been advised of the possibility of such damages.
The scope of this report and review is limited to a review of only the code presented by the Mento team and only the source code Macro notes as being within the scope of Macro’s review within this report. This report does not include an audit of the deployment scripts used to deploy the Solidity contracts in the repository corresponding to this audit. Specifically, for the avoidance of doubt, this report does not constitute investment advice, is not intended to be relied upon as investment advice, is not an endorsement of this project or team, and it is not a guarantee as to the absolute security of the project. In this report you may through hypertext or other computer links, gain access to websites operated by persons other than Macro. Such hyperlinks are provided for your reference and convenience only, and are the exclusive responsibility of such websites’ owners. You agree that Macro is not responsible for the content or operation of such websites, and that Macro shall have no liability to your or any other person or entity for the use of third party websites. Macro assumes no responsibility for the use of third party software and shall have no liability whatsoever to any person or entity for the accuracy or completeness of any outcome generated by such software.