If the stake token is minted from portfolio vault, positions from balances are not decreased
When a user mints stake tokens using their portfolio vault balance, the token balance is reduced but the associated positions' from-balances are not updated. This leaves the cross available value overstated, allowing the user to open larger positions than their remaining collateral should permit.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/106
Found by
mstpr-brainbot
Summary
When the mint stakes is minted with the portfolio vault tokens (users cross balance tokens) the entire from balances must need to change since the tokens are no longer part of users cross portfolio.
Vulnerability Detail
As we can see in the normal withdrawal flow, if any token balance is withdrawn, it affects the current positions, updating all positions accordingly, as shown in the withdraw function:
function withdraw(uint256 requestId, WithdrawParams memory params) public {
// ...
-> PositionMarginProcess.updateAllPositionFromBalanceMargin(
requestId,
params.account,
params.token,
-(params.amount.toInt256()),
""
);
}
In the MintProcess::executeMintStakeToken function flow, token balances change if the stake token is minted from portfolio vault balances:
function _transferFromAccount(address account, address token, uint256 needAmount) internal {
Account.Props storage tradeAccount = Account.load(account);
if (tradeAccount.getTokenAmount(token) < needAmount) {
revert Errors.MintFailedWithBalanceNotEnough(account, token);
}
-> tradeAccount.subTokenIgnoreUsedAmount(token, needAmount, Account.UpdateSource.TRANSFER_TO_MINT);
int256 availableValue = tradeAccount.getCrossAvailableValue();
if (availableValue < 0) {
revert Errors.MintFailedWithBalanceNotEnough(account, token);
}
}
However, this change does not update the positions from balances. This results in incorrect cross available values for the users' positions.
Impact
Since maintaining accurate balances is crucial to ensure a fair cross available value, and the above issue indicates that this balance will be disrupted, I will label it as high.
Code Snippet
Tool used
Manual Review
Recommendation
Just like withdraw function loop over the positions and update the from balances
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/51
nevillehuang
@mstpr Could you provide a more specific impact/numerical impact that justifies high severity?
mstpr
@nevillehuang
"fromBalance" is extremely important to measure users cross available value which is the value user is allowed to open cross positions. When an amount is withdrawn from users cross balance without updating its "fromBalance" the cross available value will be higher than usual although the user has lesser collateral which means user can open positions that are more than allowed respect to his/her cross portfolio balances
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
Likely the mint-from-balance path reduces the account token balance without calling the position update routine that the withdrawal path uses to keep from-balances in sync.
Impact
The cross available value becomes higher than it should be, so a user could open positions larger than their actual collateral would allow; the report labels this as high severity.
What the attacker needed
A user with a portfolio vault balance; minting stake tokens from that balance
The fix
The mint-from-balance flow should loop over the user's positions and update their from-balances, just as the withdraw function does; 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.
In this trading protocol, users have a cross-account balance that backs their open positions. When they mint stake tokens using that balance, the balance goes down but the record of how much each position relies on that balance is not adjusted. So the system thinks they still have more available collateral than they really do, letting them take on more risk than allowed.
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.