Use of outdated liability value in decreasePosition leads to account error
The protocol uses an outdated liability value when updating pool settlement after repaying a user's debt, leading to inaccurate accounting. This can cause users to be overcharged fees.
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/198
Found by
KrisRenZo
Summary
Protocol ignores repaid liability when calling LpPoolProcess.updatePnlAndUnHoldPoolAmount.
Vulnerability Detail
_settleCrossAccount() may accrue some value which may be paid in subsequent call in repayLiability, however the old liability value is used to update Pool settlement value
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/DecreasePositionProcess.sol#L119-L128
When the user's tokens increase, they will always be used to repay the liability first. For example, this occurs when tokens are deposited or when tokens increase after a position is closed and settled. It will only be triggered when there is a liability, and the settled amount has a value. addLiability refers to the newly generated liability this time, so the pool will record that it has not received the corresponding funds and mark it as unsettled. repayLiability refers to repaying the user's previous debt.
Impact
Inaccurate accounting leading to fee overcharge on users.
Code Snippet
if (cache.position.isCrossMargin) {
uint256 addLiability = _settleCrossAccount(params.requestId, accountProps, position, cache);
accountProps.repayLiability(cache.position.marginToken);
LpPoolProcess.updatePnlAndUnHoldPoolAmount(
symbolProps.stakeToken,
cache.position.marginToken,
cache.unHoldPoolAmount,
cache.poolPnlToken,
addLiability
);
Tool used
Manual Review
Recommendation
Factor in the liability that has been paid before calling LpPoolProcess::updatePnlAndUnHoldPoolAmount
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/34
nevillehuang
Request poc
sherlock-admin4
PoC requested from @Renzo1
Requests remaining: 7
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 code calls `updatePnlAndUnHoldPoolAmount` with the liability value from before repayment, instead of after repayment.
Impact
Inaccurate accounting could lead to fee overcharge on users.
What the attacker needed
A liability exists; settled amount has value
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.
In a smart contract, when a user repays a debt, the system should update the pool's records to reflect that repayment. Here, the code used the old debt amount instead of the new one, so the pool's accounting became wrong. This could make users pay more fees than they should.
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.