Incorrect implementation of the `PositionMarginProcess.updatePositionFromBalanceMargin()` function
The function `updatePositionFromBalanceMargin()` incorrectly returns early when the position's initial margin equals the margin from balance and the amount is negative, so it fails to reduce the margin from balance. This allows users to withdraw assets without reducing their position margin, enabling them to utilise more tokens than they deposited.
KupiaSec Follow with whitehair0330
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/163
Found by
KupiaSec, whitehair0330
Summary
The updatePositionFromBalanceMargin() function does nothing when position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount < 0. However, in this case, the function should actually reduce the initialMarginInUsdFromBalance of the position.
Vulnerability Detail
In the updatePositionFromBalanceMargin() function, when amount < 0, it should reduce the value of initialMarginInUsdFromBalance for the position. However, as shown at L309, the function does nothing when position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount < 0. Consequently, if users withdraw their assets, the margin amounts of the positions are not reduced accordingly. This results in users being able to utilize more tokens than they have deposited.
function updatePositionFromBalanceMargin(
Position.Props storage position,
bool needSendEvent,
uint256 requestId,
int256 amount
) public returns (uint256 changeAmount) {
309 if (position.initialMarginInUsd == position.initialMarginInUsdFromBalance || amount == 0) {
changeAmount = 0;
return 0;
}
[...]
}
Impact
As a result, users may be able to utilize more tokens than they have deposited.
Code Snippet
Tool used
Manual Review
Recommendation
The PositionMarginProcess.updatePositionFromBalanceMargin() function should be fixed as follows.
- if (position.initialMarginInUsd == position.initialMarginInUsdFromBalance || amount == 0) {
+ if ((position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount > 0) || amount == 0) {
changeAmount = 0;
return 0;
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/43
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 early-return condition uses a logical OR instead of an AND with a check on the sign of the amount, so it skips the update when the margin values are equal and the amount is negative.
Impact
Users may be able to utilise more tokens than they have deposited.
What the attacker needed
None
The fix
The condition should be changed to only return early when the margins are equal and the amount is positive, or when the amount is zero.
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.
This is a smart contract bug where a function that should update a user's margin (the collateral backing their position) does nothing in a specific case. When a user withdraws funds, the margin should decrease, but because of a wrong condition, it stays the same, letting the user use more money than they actually have.
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.