Skip to content
Business logic Audit finding Intermediate

Deleveraging can result in a zero borrowed amount while maintaining the leveraged position

A leveraged position's borrowed amount could be reduced to zero while keeping the leveraged exposure, because the deleveraging logic only unholds up to the borrowed amount. An attacker could avoid paying borrowing fees while maintaining a leveraged position.

mstpr-brainbot Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/114

Found by

mstpr-brainbot

Summary

Positions borrow the leverage amount from the pool and pay a borrowing fee for doing so. If the token price changes between the leverage update requests, an account can end up having leverage without any borrowed amount.

Vulnerability Detail

Assume Alice has:

$100 10x Isolated LONG on tokenA, traded at $1. Alice's position is worth $1000, meaning she borrowed 900 tokenA, with a holdAmount of 900.

Some time passes, and tokenA drops to $0.40. Alice decides to deleverage to 2x. Since her position is isolated and the quantity won't change, the new margin required is $500. Alice previously had $100, so she needs to add $400 worth of tokenA, which is 1000 tokenA.

1000 tokenA should be unheld from the pool. However, due to these lines in PositionMarginProcess::_executeAddMargin, it will only unhold the maximum amount, which is 900:

Solidity
function _executeAddMargin(Position.Props storage position, AddPositionMarginCache memory cache) internal {
    //..
    uint256 subHoldAmount = cache.addMarginAmount.min(position.holdPoolAmount);
    position.holdPoolAmount -= subHoldAmount;
    LpPoolProcess.updatePnlAndUnHoldPoolAmount(cache.stakeToken, position.marginToken, subHoldAmount, 0, 0);
}

Since 1000 tokenA is over the borrowed amount, only 900 tokens will be unheld from the pool, leaving the position with no holdAmount. Consequently, Alice's position will no longer incur borrowing fees. Overall, she provided a total of $500 worth of tokens, but her position is now worth $1000 in unchanged quantity.

Impact

Code Snippet

https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/PositionMarginProcess.sol#L91-L132

https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/PositionMarginProcess.sol#L340-L368

Tool used

Manual Review

Recommendation

Discussion

0xELFi02

Not a issue: No adjustment is needed; this example might not be valid because a liquidation may have already occurred.

mstpr

Not a issue: No adjustment is needed; this example might not be valid because a liquidation may have already occurred.

What if the position is cross and user has a significant amount of collateral in its portfolio? Then the account wouldn't be necessarily liquidated

0xELFi02

@mstpr After discussion with team, this is a issue, we have confirmed it. This is the fix PR: https://github.com/0xCedar/elfi-perp-contracts/pull/57

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.

The original report as published on Sherlock, by mstpr-brainbot. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 deleveraging function caps the amount to unhold at the current borrowed amount, so when the required margin increase exceeds the borrowed amount, the borrowed amount is reduced to zero without adjusting the position's leverage.

Impact

An attacker could maintain a leveraged position without paying borrowing fees, potentially causing a loss of fee revenue for the protocol.

What the attacker needed

A leveraged position; token price change; ability to deleverage

The fix

The issue was fixed by the protocol team in a pull request.

How it was found

  1. Alice opens a 10x isolated long position on tokenA at $1, borrowing 900 tokenA.
  2. The price of tokenA drops to $0.40.
  3. Alice requests to deleverage to 2x, which requires adding $400 worth of tokenA (1000 tokenA).
  4. The system only unholds 900 tokenA (the borrowed amount), leaving zero holdAmount and no borrowing fees.

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 leveraged trading, you borrow money to increase your position size and pay a fee on the borrowed amount. This report shows a flaw where, after a price change, a user could adjust their position so that the system thinks they borrowed nothing, yet they still control the same large position. That means they stop paying the borrowing fee while keeping the benefit of leverage.

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.

More by mstpr-brainbot

View all

43 more disclosed reports, best first.

More from Elfi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.