Skip to content
Business logic Audit finding Intermediate

The curve of short leverage position is not smooth and may cause users to open positions that are different from expectations

The short leverage position calculation is not smooth: at 1.0x and 1.7x leverage the resulting position size is identical, and at 1.3x it is smaller than at 1.0x. This could cause users to open positions different from their expectations.

cccz Follow

Originally disclosed on Code4rena

Submitted by cccz.

The current implementation allows users to open short leverage of 1x.

    function _leverageCheck(uint256 _leverage, bool _long) internal pure {
        uint256 _wad = WAD;
        uint256 _leverageMax = WAD.mul(3);

        _long // long can't be with 1x leverage
            ? require(
                _leverage > _wad && _leverage <= _leverageMax,
                "_leverageCheck{Long}: INVLAID LEVERAGE"
            )
            : require(
                _leverage >= _wad && _leverage <= _leverageMax,
                "_leverageCheck{Short}: INVLAID LEVERAGE"
            );
    }

When the user calls sellAllAmountWithLeverage with leverage == 1.0x , one 100% collateralization and borrowing is performed.

Consider the USDC collateralization factor of 0.7. The user uses 1000 USDC to short WBTC, leverage == 1.0x, the contract will collateralize 1000 USDC to borrow 700 USD WBTC, and then sell WBTC for 700 USDC, leaving the user with a position of 1700 USDC. When leverage == 1.3x, the contract borrows 300 USD worth of WBTC and then sells the WBTC for 300 USDC, leaving the user with a position of 1300 USDC. And when leverage == 1.7x, the contract borrows 700 USD worth of WBTC and then sells the WBTC for 700 USDC, leaving the user with a position of 1700 USDC. We note that when the leverage is 1.0x and 1.7x, the user’s position is the same, and the 1.3x position is smaller than the 1.0x position. This unsmoothed curve of short leverage position may cause the user to open a different position than expected.

Proof of Concept

The following POC shows that when the leverage is 1.0x and 1.7x, the user’s position is the same.

      it("TESTME", async function () {
        const { owner, testCoin, testStableCoin, Position } = await loadFixture(
          deployPoolsUtilityFixture
        );

        await Position.connect(owner).sellAllAmountWithLeverage(
          testStableCoin.address,
          testCoin.address,
          TUSDC_AMOUNT,
          x1
        );

        // fetch position with id 1
        const position = await Position.positions(1);
        expect(await Position.owner()).to.equal(owner.address);
        // position.asset == testStablecoin
        expect(position[0]).to.equal(testStableCoin.address);
        // position.quote == testCoin
        expect(position[1]).to.equal(testCoin.address);
        console.log("borrowedAmount : %s",position[2]);
      });
      it("TESTME2", async function () {
        const { owner, testCoin, testStableCoin, Position } = await loadFixture(
          deployPoolsUtilityFixture
        );
        const x1_7 = parseUnits("1.7");

        await Position.connect(owner).sellAllAmountWithLeverage(
          testStableCoin.address,
          testCoin.address,
          TUSDC_AMOUNT,
          x1_7
        );

        // fetch position with id 1
        const position = await Position.positions(1);
        expect(await Position.owner()).to.equal(owner.address);
        // position.asset == testStablecoin
        expect(position[0]).to.equal(testStableCoin.address);
        // position.quote == testCoin
        expect(position[1]).to.equal(testCoin.address);
        console.log("borrowedAmount : %s",position[2]);
      });
  Leverage positions Test
    Pools Utility Test
      Short positions 📉
borrowedAmount : 19444444444444444444
        ✓ TESTME
borrowedAmount : 19444444444444444444
        ✓ TESTME2

https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/utilities/poolsUtility/Position.sol#L107-L118

Recommended Mitigation Steps

Consider no longer supporting 1.0x short leverage.

daoio (Rubicon) confirmed

HickupHH3 (judge) commented:

Nice find!


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

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 leverage check allows 1.0x for shorts, and the borrowing calculation uses a formula that does not scale linearly with leverage, causing non-monotonic position sizes.

Impact

Users could open positions with different risk profiles than intended, potentially leading to unexpected losses or missed opportunities.

What the attacker needed

Access to the sellAllAmountWithLeverage function; sufficient collateral to open a position

The fix

The recommendation is to no longer support 1.0x short leverage.

How it was found

  1. Call sellAllAmountWithLeverage with leverage 1.0x and record the borrowed amount.
  2. Call sellAllAmountWithLeverage with leverage 1.7x and record the borrowed amount.
  3. Observe that both calls result in the same borrowed amount.

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. The code that calculates how much to borrow for short positions has a flaw: the resulting position size does not increase smoothly with leverage. For example, using 1.0x and 1.7x leverage gives the same position size, which is unexpected and could lead to losses if users rely on the stated 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 cccz

View all

104 more disclosed reports, best first.

More from Rubicon v2

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.