Improper implementation of the `PositionMarginProcess.updatePositionFromBalanceMargin()` function
The function that updates a position's margin balance uses percentages of the initial margin rather than the current price of the margin token. This allows a user to update their position using a stale, higher price even when the token's price has fallen.
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/159
Found by
0x486776
Summary
The updates to position values are not based on the current price of the marginToken.
Vulnerability Detail
As shown in the code at L314 and L318, all calculations are based on percentages relative to the maximum values. They do not factor in the current price of the marginToken. Consequently, even if the current marginToken price is significantly lower than when the position was last updated, users can still update their position using the higher price.
function updatePositionFromBalanceMargin(
Position.Props storage position,
bool needSendEvent,
uint256 requestId,
int256 amount
) public returns (uint256 changeAmount) {
if (position.initialMarginInUsd == position.initialMarginInUsdFromBalance || amount == 0) {
changeAmount = 0;
return 0;
}
if (amount > 0) {
314 uint256 borrowMargin = (position.initialMarginInUsd - position.initialMarginInUsdFromBalance)
.mul(position.initialMargin)
.div(position.initialMarginInUsd);
changeAmount = amount.toUint256().min(borrowMargin);
318 position.initialMarginInUsdFromBalance += changeAmount.mul(position.initialMarginInUsd).div(
position.initialMargin
);
} else {
322 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();
}
}
if (needSendEvent && changeAmount > 0) {
position.emitPositionUpdateEvent(requestId, Position.PositionUpdateFrom.DEPOSIT, 0);
}
}
Impact
Users can update their positions' initialMarginInUsdFromBalance values using a price higher than the current price of the marginToken.
Code Snippet
Tool used
Manual Review
Recommendation
The PositionMarginProcess.updatePositionFromBalanceMargin() function should be based on the current price of the marginToken.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/57
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 function calculates margin changes using only the initial margin amounts and percentages, without incorporating the current price of the margin token.
Impact
Users can update their positions' `initialMarginInUsdFromBalance` values using a price higher than the current price of the `marginToken`.
What the attacker needed
None
The fix
The function should be based on the current price of the margin token, and 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 trading system, the value of your collateral (margin) should be recalculated when its price changes. Here, the code that adjusts your margin balance ignores the current price and uses the original price, so you can pretend your collateral is worth more than it really is.
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.