Skip to content
DoS Audit finding Intermediate

Both buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert

The functions buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert because the internal _maxBorrow function requires positive liquidity, but a newly created Position contract has zero liquidity. An attacker can transfer a tiny amount of bathToken to the Position contract to trigger the revert, preventing users from opening leveraged positions.

nobody2018 Follow with Bauer

Originally disclosed on Code4rena

Submitted by nobody2018, also found by Bauer.

https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L545 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L306-L319

Both buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert. So all users cannot open a leveraged position.

Proof of Concept

openPosition is the entry point for users to open long/short positions, internally calling the _borrowLimit function to calculate an amount of iterations needed to reach desired amount with leverage. The logic of this function is: if the collateral has been provided, then the _maxBorrow function will be called to calculate the maximum amount available to borrow from _bathToken market, and then the returned result will be added to the _loopBorrowed variable.

// check if collateral was already supplied
        uint256 _minted = IERC20(_bathToken).balanceOf(address(this));
    // how much is borrowed on a current loop
        uint256 _loopBorrowed;

        while (_assetAmount <= _desiredAmount) {
            if (_limit == 0) {
        // if collateral already provided
                if (_minted != 0) {
                    uint256 _max = _maxBorrow(_bathToken);

            // take into account previous collateral
                    _loopBorrowed = wmul(_assetAmount, _collateralFactor).add(
                        _max
                    );

Next, look at the code of _maxBorrow:

function _maxBorrow(
        address _bathToken
    ) internal view returns (uint256 _max) {
        (uint256 _err, uint256 _liq, uint256 _shortfall) = comptroller
            .getAccountLiquidity(address(this));		//if address(this) is new user of comptroller, _liq always equal to 0

        require(_err == 0, "_maxBorrow: ERROR");
        require(_liq > 0, "_maxBorrow: LIQUIDITY == 0");	//if _liq equals to 0, tx will revert.
        require(_shortfall == 0, "_maxBorrow: SHORTFALL != 0");

        uint256 _price = oracle.getUnderlyingPrice(CToken(_bathToken));
        _max = (_liq.mul(10 ** 18)).div(_price);
        require(_max > 0, "_maxBorrow: can't borrow 0");
    }

To make the _liq returned by comptroller.getAccountLiquidity(address(this)) be 0, then address(this) must not provide any collateral. In other words, this contract is a new user to the comptroller. Obviously, the Position contract created by the user meets this condition.

The condition for calling _maxBorrow is that IERC20(_bathToken).balanceOf(address(this)) returns a non-zero value. A newly created Position contract does not have any tokens. However, we can transfer 1wei _bathToken to it. This allows the code to execute to _maxBorrow and revert.

There are two ways to transfer _bathToken to the newly created Position:

  1. Front-run buyAllAmountWithLeverage or sellAllAmountWithLeverage.
  2. Once the Position is created, transfer all _bathTokens supported by BathHouseV2 to it. The amount transferred is 1wei.

In fact, _maxBorrow will also revert in normal scenario. Position borrows other tokens resulting in _liq being 0.

Recommended Mitigation Steps

We should use comptroller.getAccountLiquidity instead of the balance of _bathToken as the condition for calling _maxBorrow.

--- a/contracts/utilities/poolsUtility/Position.sol
+++ b/contracts/utilities/poolsUtility/Position.sol
@@ -534,7 +534,7 @@ contract Position is Ownable, DSMath {
         uint256 _desiredAmount = wmul(_assetAmount, _leverage);

         // check if collateral was already supplied
-        uint256 _minted = IERC20(_bathToken).balanceOf(address(this));
+        (,uint256 _minted,) = comptroller.getAccountLiquidity(address(this));
        // how much is borrowed on a current loop
         uint256 _loopBorrowed;

daoio (Rubicon) confirmed

HickupHH3 (judge) commented:

Medium: loss of functionality due to griefing from external party.

Marking as best because it mentions an additional attack path compared to counterpart.


The original report as published on Code4rena, by nobody2018. 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 code uses the bathToken balance as a condition to call _maxBorrow, but _maxBorrow requires positive liquidity, which a new Position contract does not have.

Impact

Users cannot open leveraged positions, and an attacker can grief the system by transferring a tiny amount of bathToken to a Position contract.

What the attacker needed

Attacker can front-run or transfer tokens; Position contract is newly created

The fix

The code should use comptroller.getAccountLiquidity instead of the bathToken balance as the condition for calling _maxBorrow.

How it was found

  1. Attacker front-runs the buyAllAmountWithLeverage or sellAllAmountWithLeverage transaction.
  2. Attacker transfers 1 wei of bathToken to the newly created Position contract.
  3. The Position contract's balance becomes non-zero, so it calls _maxBorrow.
  4. _maxBorrow calls comptroller.getAccountLiquidity, which returns 0 liquidity for the new contract.
  5. _maxBorrow reverts because liquidity is 0, causing the entire transaction to revert.

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 opens leveraged positions always fails. The contract checks if it has any tokens and then tries to borrow more, but the borrowing check fails because the contract has no collateral yet. An attacker can send a tiny amount of a token to the contract to make it fail even earlier, blocking all users from using the feature.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by nobody2018

View all

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

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.