Skip to content
Business logic Audit finding Intermediate

Lack of timely update borrowing fee when update position's margin

The borrowing fee for an isolated position is calculated using the position's current leverage, but when a trader adds margin, the leverage decreases without first updating the accumulated borrowing fee. An attacker can open a high-leverage position, wait, then add margin to lower leverage and pay less borrowing fee than owed.

CL001 Follow with ZeroTrust, jennifer37 and 1 other

Originally disclosed on Sherlock

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

The protocol has acknowledged this issue.

Found by

CL001, ZeroTrust, jennifer37, mstpr-brainbot

Summary

The borrow fee is related with initialMargin and leverage. When initialMargin/leverage changes, we need to update borrow fees.

Vulnerability Detail

When traders want to update isolated position's margin, the keepers will execute executeUpdatePositionMarginRequest to update positions' margin. The operation will keep this isolated position's whole position size the same as before. It means that if the traders increase margin for one position, the position's leverage will be decreased.

JavaScript
    function _executeAddMargin(Position.Props storage position, AddPositionMarginCache memory cache) internal {
        //Cannot change position size, so cannot exceed the position size
       ......
        position.initialMargin += cache.addMarginAmount;
        if (cache.isCrossMargin) {
            // here leverage is updated leverage. Keep qty still
            position.initialMarginInUsd = CalUtils.divRate(position.qty, position.leverage);
            position.initialMarginInUsdFromBalance += cache.addInitialMarginFromBalance;
        } else {
            //Isolation mode, when add margin, the leverage will decrease.
            position.initialMarginInUsd += CalUtils.tokenToUsd(
                cache.addMarginAmount,
                cache.marginTokenDecimals,
                cache.marginTokenPrice
            );
            position.leverage = CalUtils.divRate(position.qty, position.initialMarginInUsd);
            position.initialMarginInUsdFromBalance = position.initialMarginInUsd;
        }

The borrowing fee is related with initialMargin and position's leverage. So if we increase one position's margin, our borrow fee should decrease from now. Otherwise, our borrow fee should increase from now. The vulnerability is that the traders' borrowing fee is not accurate. Hackers can make use of this vulnerability to pay less borrowing fee. For example:

  • Alice starts one isolated position with high leverage. The borrowing fees start to be accumulated from now.
  • After a long time, when Alice wants to close her position, Alice can add margin to decrease her position's leverage.
  • When Alice close her position. she will pay less borrow fees that she should because currently the leverage is quite low.
JavaScript
    function updateBorrowingFee(Position.Props storage position, address stakeToken) public {
        uint256 cumulativeBorrowingFeePerToken = MarketQueryProcess.getCumulativeBorrowingFeePerToken(
            stakeToken,
            position.isLong,
            position.marginToken
        );
        uint256 realizedBorrowingFeeDelta = CalUtils.mulSmallRate(
            CalUtils.mulRate(position.initialMargin, position.leverage - CalUtils.RATE_PRECISION),
            cumulativeBorrowingFeePerToken - position.positionFee.openBorrowingFeePerToken
        );
        ......
    }

Impact

Traders can pay less borrowing fees than they should.

Code Snippet

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

Tool used

Manual Review

Recommendation

Timely update borrowing fees.

The original report as published on Sherlock, by CL001. 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 borrowing fee is not updated before the position's leverage is changed when margin is added, so the fee calculation uses the new lower leverage for the entire period.

Impact

Traders can pay less borrowing fees than they should.

What the attacker needed

Ability to open an isolated position; ability to add margin to the position

The fix

The borrowing fee should be updated before any change to the position's margin or leverage.

How it was found

  1. Alice opens an isolated position with high leverage, and borrowing fees start accumulating.
  2. After a long time, Alice adds margin to the position, which decreases its leverage.
  3. Alice closes the position and pays less borrowing fee than she should because the fee is calculated with the now-low leverage.

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.

Borrowing fees are charges for keeping a leveraged trading position open. The fee depends on how much you borrowed, which is linked to leverage. The bug is that when you add money to your position, the system lowers your leverage but forgets to charge the higher fee for the time you held the risky position, so you pay less.

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 CL001

View all

4 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.