`updatePositionFromBalanceMargin` function returns "0" if amount to be updated is negative
The updatePositionFromBalanceMargin function sets a position's initialMarginInUsdFromBalance to zero before calculating the change amount, causing the change amount to always be zero when the amount to update is negative. This results in other positions' fromBalance being incorrectly reset, allowing the account to borrow more than it should.
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/36
Found by
KrisRenZo, KupiaSec, chaduke, jennifer37, mstpr-brainbot, nikhil840096, whitehair0330
Summary
When a cross position is closed all the other cross positions "fromBalance"s updated. If the amount to be updated is negative then the other positions "fromBalance" should be decreased to lower down their cross available value. However, the function logic always returns "0" prior to storage update before the return statement.
Vulnerability Detail
Say Bob has 3 positions as follows: WBTC SHORT (margin: 100, marginFromBalance: 100, initialMarginInUsd: 100, initialMarginInUsdFromBalance: 100) SOL SHORT (margin: 100, marginFromBalance: 50, initialMarginInUsd: 100, initialMarginInUsdFromBalance: 50) WETH SHORT (margin: 100, marginFromBalance 90, initialMarginInUsd: 100, initialMarginInUsdFromBalance: 90)
Say the WBTC position is closed in loss such that there is a negative settledMargin. The changeToken will also be negative. Say the value for changeToken is -110.
Below lines will be executed in PositionMarginProcess::updatePositionFromBalanceMargin() function since the changeToken is negative. For SOL the addBorrowMarginInUsd will be 110 * 100 / 100 = 110. Since this value is higher than position.initialMarginInUsdFromBalance the first if check will be executed and position.initialMarginInUsdFromBalance will be "0". Then, the actual changeAmount will be calculated right after, normally this value should be the 50 * 100 / 100 = 50 token. However, because the "position" is a storage pointer and its value is set to "0" before the changeAmount calculation, changeAmount calculation will also be "0". That means that when the loop goes to the WETH position, instead of only decreasing 60 tokens (110-50) it will also reset the entire position.initialMarginInUsdFromBalance for the WETH position.
else {
uint256 addBorrowMarginInUsd = (-amount).toUint256().mul(position.initialMarginInUsd).div(
position.initialMargin
);
if (position.initialMarginInUsdFromBalance <= addBorrowMarginInUsd) {
position.initialMarginInUsdFromBalance = 0;
changeAmount = position.initialMarginInUsdFromBalance.mul(position.initialMargin).div(
position.initialMarginInUsd
);
} else {
position.initialMarginInUsdFromBalance -= addBorrowMarginInUsd;
changeAmount = (-amount).toUint256();
}
}
Impact
Entire "from balance" will be off. Account will have a lower "from balance" which means that the account can borrow more although it shouldn't be. No coded poc here because the issue is clear from text and easy to spot.
Code Snippet
Tool used
Manual Review
Recommendation
Change the order of these lines to this:
changeAmount = position.initialMarginInUsdFromBalance.mul(position.initialMargin).div(position.initialMarginInUsd);
position.initialMarginInUsdFromBalance = 0;
Discussion
0xELFi
The same as: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/3
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/15
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 code sets position.initialMarginInUsdFromBalance to 0 before using it to compute changeAmount, so changeAmount becomes 0 instead of the intended value.
Impact
The entire 'from balance' will be off, giving the account a lower from balance and allowing it to borrow more than it should.
What the attacker needed
Multiple cross positions; closing a position at a loss
The fix
The fix is to calculate changeAmount before setting initialMarginInUsdFromBalance to 0; 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 a smart contract, a function that updates margin balances has a bug: it zeroes out a value before using it to calculate how much to subtract from other positions. This means when closing a position at a loss, the system incorrectly wipes out the 'from balance' of other positions, letting the user borrow more 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.