Security Audit
October 17, 2024
Version 1.0.0
Presented by 0xMacro
This document includes the results of the security audit for Infinex's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from October 8th to October 11th, 2024.
The purpose of this audit is to review the source code of certain Infinex 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 |
---|---|---|---|---|
Medium | 2 | - | - | 2 |
Low | 5 | - | - | 5 |
Code Quality | 9 | - | - | 9 |
Gas Optimization | 1 | - | - | 1 |
Infinex 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:
b012e54c8c10e88eef5c825b36c1ecfd1385c9b0
ef135dfe1ef8a0523ca0817524d5dc67c9dacd6a
d6ca8e9bbb2138a51b9dbe3933895c16991e9d11
1af229e71aa1867125da3830f0e4b127578bb8ba
Specifically, we audited the following contracts within this repository:
Source Code | SHA256 |
---|---|
src/reward-campaign/IRewardCampaign.sol |
|
src/reward-campaign/RewardCampaign.sol |
|
src/reward-campaign/RewardCampaignStorage.sol |
|
src/forwarder/ERC2771BaseUpgradeable.sol |
|
src/blast-points/BlastPointsDistributor.sol |
|
src/blast-points/IBlastPointsDistributor.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.
totalRewards
cannot be allocated when overriding vesting entries
claimable()
view function is not returning proper value when the campaign has not started yet
claim()
function or the claimToAllowlistedWithdrawalAddress()
function can be called multiple times
amount = 0
address(0)
in the RewardCampaign.initialize()
function
id
member in the Campaign
struct can be changed to isDeleted
member
_setVestingEntry
function
fundCampaign
can be called after campaign were started
addTrustedForwarder()
and removeTrustedForwarder()
functions from the ERC2771BaseUpgradeable
contract is not mandatory
setVestingEntries()
function
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. |
totalRewards
cannot be allocated when overriding vesting entries
When setting vesting entries, the _setVestingEntry
function checks that the total allocated amount doesn't exceed the totalReward
amount:
if (_amount + campaign.totalAllocated > campaign.totalReward) revert AllocationOverTotalReward();
Reference: RewardCampaign.sol#L359
This check works as intended for new accounts. However, it fails when overriding an existing vesting entry. The issue arises because campaign.totalAllocated
includes the old entry's amount, potentially causing the transaction to revert even when the total allocation doesn't actually exceed totalReward
.
Consider this scenario: A campaign has a totalReward
of 100, with 95 already allocated, including 10 for accountA
. If the vestingEntryManager tries to change accountA
's allocation to 15, the check fails:
15 + 95 > 100
(true, causing revert)
To fix this, subtract the old amount (= 10) from totalAllocated
:
15 + (95 - 10) > 100
(false, allowing the update)
Remediation to Consider
Modify the above check to account for the previously allocated amount when overriding an existing entry.
In order to delete the campaign, the owner must call to deleteCampaign()
, which basically sets the campaign.id
value to zero:
function deleteCampaign(uint32 _campaignId) external onlyOwner {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
if (_campaignId == 0 || campaign.id == 0) revert InvalidCampaignId();
emit CampaignDeleted(_campaignId);
RewardCampaignStorage._deleteCampaign(_campaignId);
if (campaign.funded) {
IERC20(campaign.token).safeTransfer(msg.sender, campaign.totalReward - campaign.totalClaimed);
}
}
Reference: RewardCampaign.sol#L164-L175
function _deleteCampaign(uint32 _campaignId) internal {
getStorage().campaigns[_campaignId].id = 0;
}
Reference: RewardCampaignStorage.sol#L110-L112
In the fundCampaign()
, startCampaign()
, and all the claiming functions don’t restrict whether the campaign is deleted. As a result, a deleted campaign can still be funded, started, and can even be claimed by users when the campaign is deleted
Remediations to Consider
Consider disallowing the call to other functions with a particular campaign if the campaign is deleted
RewardCampaign doesn't properly account for tokens that apply a transfer tax. When such a token is used, the actual amount transferred in the fundCampaign
function is less than the specified _amount
. This discrepancy leads to an incorrect totalReward
calculation, potentially preventing users from claiming rewards as the contract may run out of funds.
Remediation to Consider
To accurately handle fee-on-transfer tokens, calculate the contract's balance before and after the transfer. Use this difference, rather than the specified amount, to update the campaign's total reward.
claimable()
view function is not returning proper value when the campaign has not started yet
In the RewardCampaign.claimable()
function, the return value will be the amount of token that can be claimed for a particular vesting entry at that moment:
function claimable(uint32 _campaignId, address _account) external view returns (uint256) {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
IRewardCampaign.VestingEntry memory vestingEntry = RewardCampaignStorage._getVestingEntry(_campaignId, _account);
if (vestingEntry.amount == 0) return 0;
uint256 claimableAmount = _claimable(campaign.startDate, campaign.vestingCliff, campaign.vestingDuration, vestingEntry.amount);
uint256 availableToClaim = claimableAmount - vestingEntry.claimed;
return availableToClaim;
}
Reference: RewardCampaign.sol#L120-L127
When the campaign is not started, the campaign’s startDate
is still 0. Theoretically, the claimable()
function should return zero at that time, since no vesting entry can be claimed. But in reality, it will return a maximum amount of token of the vesting entry:
function _claimable(uint256 _start, uint256 _cliff, uint256 _duration, uint256 _amount) internal view returns (uint256) {
if (block.timestamp < _start + _cliff) {
return 0;
} else if (block.timestamp >= _start + _cliff + _duration) {
return _amount; //<@@ will go to this case because _start == 0
} else {
return _amount * (block.timestamp - (_start + _cliff)) / _duration;
}
}
Remediations to Consider
Consider making the claimable()
return zero when the campaign has not started yet
function claimable(uint32 _campaignId, address _account) external view returns (uint256) {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
+ if (campaign.startDate == 0) return 0;
IRewardCampaign.VestingEntry memory vestingEntry = RewardCampaignStorage._getVestingEntry(_campaignId, _account);
if (vestingEntry.amount == 0) return 0;
uint256 claimableAmount = _claimable(campaign.startDate, campaign.vestingCliff, campaign.vestingDuration, vestingEntry.amount);
uint256 availableToClaim = claimableAmount - vestingEntry.claimed;
return availableToClaim;
}
The _claim()
function doesn’t follow check-effect-interaction (CEI) pattern:
function _claim(uint32 _campaignId, address _account, address _destination) internal {
...
IERC20(campaign.token).safeTransfer(_destination, availableToClaim); <@@@ REENTRANCY
RewardCampaignStorage._setVestingEntryClaimed(_campaignId, _account, availableToClaim);
RewardCampaignStorage._setCampaignTotalClaimed(_campaignId, availableToClaim);
...
}
Reference: RewardCampaign.sol#L330-L346
As a result, the function is vulnerable to reentrancy when the reward token of that campaign supports the ERC777 standard. The attacker can claim many times without changing states, which leads to draining all the reward token that belong to the RewardCampaign
contract
The fundCampaign()
function is also not following the CEI pattern, which is also vulnerable to reentrancy when the reward token of that campaign supports the ERC777 standard. Even though the impact is not severe, it’s better still to follow the CEI pattern
function fundCampaign(uint32 _campaignId, uint256 _amount) external {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
if (_ERC2771MsgSender() != campaign.rewarder) revert InvalidRewarder();
if (campaign.funded) revert CampaignAlreadyFunded();
if (_amount != campaign.totalReward) revert InvalidAmount();
emit CampaignFunded(_campaignId, campaign.rewarder, _amount);
// slither-disable-next-line arbitrary-send-erc20
IERC20(campaign.token).safeTransferFrom(_ERC2771MsgSender(), address(this), _amount);
RewardCampaignStorage._setCampaignFunded(_campaignId, true);
}
Reference: RewardCampaign.sol#L182-L193
Remediations to Consider
Consider following CEI pattern in _claim()
and fundCampaign()
functions
The RewardCampaign
contract allows claiming reward tokens to an Infinex account with the account’s sudo signature, using either the claim()
function or the claimToAllowlistedWithdrawalAddress()
function with an EIP1271 signature. In the claim()
function, the hash structure is the combination of _campaignId
with uint32
type and _account
with address
type:
bytes32 requestHash = keccak256(abi.encodePacked(_campaignId, _account));
Reference: RewardCampaign.sol#L299
In claimToAllowlistedWithdrawalAddress()
function, the hash structure is the combination of _campaignId
with uint32
type, _account
with address
type, and _withdrawalAddress
with address
type:
bytes32 requestHash = keccak256(abi.encodePacked(_campaignId, _account, _withdrawalAddress));
Reference: RewardCampaign.sol#L324
The hash structures of these 2 functions are relatively simple. In the future, when more contracts allow the EIP1271 standard for Infinex accounts, it could lead to a single signature being a valid input for different functions from different contracts, potentially leading to unpredictable behavior.
Remediations to Consider
Consider including the RewardCampaign
address and corresponding function selector in the hashing structure of the signature
claim()
function or the claimToAllowlistedWithdrawalAddress()
function can be called multiple times
The RewardCampaign
contract allows claiming reward tokens to an Infinex account with the account’s sudo signature, using either the claim()
function or the claimToAllowlistedWithdrawalAddress()
function with an EIP1271 signature. These functions don’t have any mechanism to prevent the same signature from being reused. Even though the reward tokens will be sent to the correct account, it is still a potential griefing vector and we recommend preventing signatures from being replay.
Remediations to Consider
Consider including a nonce mechanism in the hashing structure of the signature
amount = 0
The setVestingEntry
function lacks a check to ensure the _amount
parameter is greater than zero. Consequently, claiming for an entry with a zero amount will fail, triggering an InvalidVestingEntry()
error.
Remediation to Consider
Although this doesn't pose a security risk, it's advisable to prevent the creation of invalid entries in the setVestingEntry
function.
address(0)
in the RewardCampaign.initialize()
function
In the RewardCampaign.initialize()
function, trusted forwarder can be set as address(0)
function initialize(address _owner, address _trustedForwarder) public initializer {
OwnableUpgradeable.__Ownable_init(_owner);
ERC2771Context.initialize(_trustedForwarder);
}
Reference: RewardCampaign.sol#L55-L58
This behavior is not harmful because the owner can change trusted forwarder anytime, but since addTrustedForwarder()
and removeTrustedForwarder()
are not allowing trustedForwarder
as address(0)
, it makes sense to also disallow in the initialize()
function for consistency state
Remediations to Consider
Consider disallowing _trustedForwarder
to address(0)
in the initialize()
function
function initialize(address _owner, address _trustedForwarder) public initializer {
OwnableUpgradeable.__Ownable_init(_owner);
+ if (_trustedForwarder == address(0)) revert Error.NullAddress();
ERC2771Context.initialize(_trustedForwarder);
}
id
member in the Campaign
struct can be changed to isDeleted
member
The id
member in the Campaign
struct is non-zero when the campaign exists and is set to zero if the campaign is not created or deleted. The id
is already stored as a key in the campaigns
mapping, hence it’s redundant to have an id
member in the Campaign
struct.
Consider changing the id
member in the Campaign
struct to isDeleted
member with a boolean type for better clarity and to reduce state redundant
_setVestingEntry
function
In the RewardCampaignStorage._setVestingEntry
function, there’s a mechanism to prevent the underflow error:
if (vestingEntry.amount > _amount) {
newTotalAllocated = campaign.totalAllocated - (vestingEntry.amount - _amount);
} else {
newTotalAllocated = campaign.totalAllocated + (_amount - vestingEntry.amount);
}
Reference: RewardCampaignStorage.sol#L160-L164
This logic is redundant due to the following invariant: the totalAllocated
of the campaign is equal to the sum of all the vestingEntry
amounts of the same campaign. This means the totalAllocated
of the campaign is always greater than or equal to any single vestingEntry
amount of the same campaign. Therefore, it's impossible for campaign.totalAllocated - vestingEntry.amount
to underflow.
Remediations to Consider
Consider removing the underflow mechanism for better simplicity:
function _setVestingEntry(uint32 _campaignId, address _account, uint256 _amount) internal {
IRewardCampaign.Campaign memory campaign = getStorage().campaigns[_campaignId];
IRewardCampaign.VestingEntry memory vestingEntry = getStorage().vestingEntries[_campaignId][_account];
uint256 newTotalAllocated;
- if (vestingEntry.amount > _amount) {
- newTotalAllocated = campaign.totalAllocated - (vestingEntry.amount - _amount);
- } else {
- newTotalAllocated = campaign.totalAllocated + (_amount - vestingEntry.amount);
- }
+ newTotalAllocated = campaign.totalAllocated + _amount - vestingEntry.amount;
getStorage().campaigns[_campaignId].totalAllocated = newTotalAllocated;
getStorage().vestingEntries[_campaignId][_account] = IRewardCampaign.VestingEntry({ amount: _amount, claimed: 0 });
}
fundCampaign
can be called after campaign were started
After a campaign has been started, no more vesting entries can be added to the campaign. Thus, there is no need to add more funds to the campaign via fundCampaign
.
Remediation to Consider
Consider preventing the addition of funds to campaigns that have already started.
A Rewarder and VestingManager address can be set via setCampaignRewarder
and setCampaignVestingEntryManager
for campaigns. Ideally, this should only be possible for campaigns that haven't started yet. However, the current implementation allows setting or changing these addresses even for ongoing campaigns.
Remediation to Consider
Restrict the ability to set Rewarder and VestingManager addresses to campaigns that have not yet begun.
addTrustedForwarder()
and removeTrustedForwarder()
functions from the ERC2771BaseUpgradeable
contract is not mandatory
In the ERC2771BaseUpgradeable
abstract contract, there are addTrustedForwarder()
and removeTrustedForwarder()
functions that the child contract should override. However, because these functions contain empty braces { }
, the compiler treats them as implemented with empty logic. As a result, the compiler will not throw any error when a child contract does not override the addTrustedForwarder()
and removeTrustedForwarder()
functions from the ERC2771BaseUpgradeable
contract. Child contracts that forget to override these functions may not be able to add or remove trusted forwarders.
function addTrustedForwarder(address _trustedForwarder) external virtual returns (bool) { }
function removeTrustedForwarder(address _trustedForwarder) external virtual returns (bool) { }
Reference: ERC2771BaseUpgradeable.sol#L54-L61
Remediations to Consider
Consider changing the code as follows to make function overriding mandatory for child contracts:
- function addTrustedForwarder(address _trustedForwarder) external virtual returns (bool) { }
+ function addTrustedForwarder(address _trustedForwarder) external virtual returns (bool);
- function removeTrustedForwarder(address _trustedForwarder) external virtual returns (bool) { }
+ function removeTrustedForwarder(address _trustedForwarder) external virtual returns (bool);
In the RewardCampaign
contract, the rewarder of the campaign is set initially in the createCampaign()
function by the owner, and can be changed later with the setCampaignRewarder()
function by the owner. Only the rewarder of the campaign can fund the corresponding campaign via the fundCampaign()
function, which is too strict.
Remediation to Consider
Consider removing the rewarder mechanism and making fundCampaign()
function permissionless for more flexibility.
Topic | Validation |
---|---|
Impact | Low |
According to the Blast’s documentation, the Blast Point operator must be an EOA:
An operator is an EOA (externally owned account) whose private key is accessible to an internet-connected server.
However, in the BlastPointDistributor
contract, there's no check to verify whether the operator is an EOA or not. Consider adding a validation to restrict the operator to EOA addresses only.
setVestingEntries()
function
In the setVestingEntries()
function, _setVestingEntry()
internal function is called multiple times in the loop with the same _campaignId
argument:
function setVestingEntries(uint32 _campaignId, address[] calldata _accounts, uint256[] calldata _amounts)
external
onlyVestingEntryManager
{
if (_accounts.length != _amounts.length) revert Error.InvalidLength();
for (uint256 i = 0; i < _accounts.length; i++) {
_setVestingEntry(_campaignId, _accounts[i], _amounts[i]);
}
}
Reference: RewardCampaign.sol#L228-L236
In the _setVestingEntry()
function, the _campaignId
parameter is restricted by these two requirements:
function _setVestingEntry(uint32 _campaignId, address _account, uint256 _amount) internal {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
if (_campaignId == 0 || campaign.id == 0) revert InvalidCampaignId();
if (campaign.startDate > 0) revert CampaignHasStarted();
...
}
Reference: RewardCampaign.sol#L313-L322
While calling _setVestingEntry()
multiple times through the setVestingEntries()
function, the same two requirements are checked, which is redundant and should be only checked once throughout the setVestingEntries()
function
Remediations to Consider
Consider moving these two requirements from _setVestingEntry()
function to the higher functions, which is setVestingEntry()
and setVestingEntries()
functions
function setVestingEntry(uint32 _campaignId, address _account, uint256 _amount) external onlyVestingEntryManager {
+ IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
+ if (_campaignId == 0 || campaign.id == 0) revert InvalidCampaignId();
+ if (campaign.startDate > 0) revert CampaignHasStarted();
_setVestingEntry(_campaignId, _account, _amount);
}
function setVestingEntries(uint32 _campaignId, address[] calldata _accounts, uint256[] calldata _amounts)
external
onlyVestingEntryManager
{
if (_accounts.length != _amounts.length) revert Error.InvalidLength();
+ IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
+ if (_campaignId == 0 || campaign.id == 0) revert InvalidCampaignId();
+ if (campaign.startDate > 0) revert CampaignHasStarted();
for (uint256 i = 0; i < _accounts.length; i++) {
_setVestingEntry(_campaignId, _accounts[i], _amounts[i]);
}
}
function _setVestingEntry(uint32 _campaignId, address _account, uint256 _amount) internal {
IRewardCampaign.Campaign memory campaign = RewardCampaignStorage._getCampaign(_campaignId);
- if (_campaignId == 0 || campaign.id == 0) revert InvalidCampaignId();
- if (campaign.startDate > 0) revert CampaignHasStarted();
...
}
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 Infinex 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.