Submitting mint request using user's trading balance and cancelling it will not refund tokens back to trading account
The mint request cancellation function only refunds tokens that came from the user's wallet, not those taken from the trading account balance. An attacker could lose tokens if a mint request funded from the trading balance is cancelled.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/77
Found by
eeshenggoh
Summary
The Elfi protocol provides a feature that allows users to transfer a portion of their tokens after depositing them into a trading account, helping them manage their finances more effectively. According to the dev team:
When the keeper fails to execute the execute method, it will call the corresponding cancel method to revoke this request
The problem arises if the mint request uses the trading balance to get elfi tokens.
Vulnerability Detail
This is the logic where users can use trading account balances to obtain elfi tokens.
function _mintStakeToken(Mint.Request memory mintRequest) internal returns (uint256 stakeAmount) {
-- SNIP --
if (mintRequest.requestTokenAmount > mintRequest.walletRequestTokenAmount) {
_transferFromAccount(
mintRequest.account,
mintRequest.requestToken,
mintRequest.requestTokenAmount - mintRequest.walletRequestTokenAmount
);
}
-- SNIP --
The MintProcess::cancelMintStakeToken refunds the tokens sent from user EOA to the vault. However, the trading balance account isn't refunded if mint requests are executed and funded by the trading account.
function cancelMintStakeToken(uint256 requestId, Mint.Request memory mintRequest, bytes32 reasonCode) external {
if (mintRequest.walletRequestTokenAmount > 0) { //@audit missing implementation for trading account
VaultProcess.transferOut(
mintRequest.isCollateral
? IVault(address(this)).getPortfolioVaultAddress() // ok
: IVault(address(this)).getLpVaultAddress(),// ok
mintRequest.requestToken,
mintRequest.account, //transfer to user
mintRequest.walletRequestTokenAmount
);
}
Mint.remove(requestId);
emit CancelMintEvent(requestId, mintRequest, reasonCode);
}
Impact
Users who staked with trading balance will lose tokens if mint requests are cancelled.
Code Snippet
Tool used
Manual Review
Recommendation
The development team can choose to implement either refund to the EOA (Externally Owned Account) or to the trading account.
The following solution, updates the trading balance account:
function cancelMintStakeToken(uint256 requestId, Mint.Request memory mintRequest, bytes32 reasonCode) external {
+ if (mintRequest.requestTokenAmount > mintRequest.walletRequestTokenAmount) {
+ Account.Props storage accountProps = Account.load(mintRequest.account);
+ accountProps.checkExists();
+ accountProps.addToken(mintRequest.requstToken, mintrequest.requestTokenAmount);
+ }
+ else (mintRequest.walletRequestTokenAmount > 0) { //@audit missing implementation for trading account
VaultProcess.transferOut(
mintRequest.isCollateral
? IVault(address(this)).getPortfolioVaultAddress() // ok
: IVault(address(this)).getLpVaultAddress(),// ok
mintRequest.requestToken,
mintRequest.account, //transfer to user
mintRequest.walletRequestTokenAmount
);
}
Mint.remove(requestId);
emit CancelMintEvent(requestId, mintRequest, reasonCode);
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/53
sherlock-admin2
The Lead Senior Watson signed off on the fix.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The cancelMintStakeToken function only refunds the walletRequestTokenAmount to the user's wallet and does not restore the portion of tokens that were transferred from the trading account balance.
What the attacker needed
User has a trading account balance; mint request uses trading balance; mint request is cancelled
The fix
The protocol team fixed this issue in a pull request.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you deposit money into a trading account, you can use that balance to mint new tokens. If the minting process fails and is cancelled, the system is supposed to give your money back. But the code only refunds money that came directly from your wallet, not from your trading balance, so you lose those funds.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.