Skip to content
Business logic Audit finding Intermediate

Position._borrowLimit doesn’t use exisiting collateral in case if user doesn’t have any _bathToken

The borrow-limit calculation only counts collateral held in the specific bathToken being borrowed against, and ignores collateral the user holds in other bathTokens. As a result, a user with existing collateral elsewhere is treated as having none, so the leverage loop under-borrows and the position is smaller than intended.

rvierdiiev Follow

Originally disclosed on Code4rena

Submitted by rvierdiiev.

Position._borrowLimit doesn’t use exisiting collateral in case if user doesn’t have any _bathToken.

Proof of Concept

Position._borrowLimit function is used to calculate how many times user needs to make borrow loop and how many percent of maximum allowed amount to borrow. https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L526-L583

    function _borrowLimit(
        address _bathToken,
        address _asset,
        uint256 _assetAmount,
        uint256 _leverage
    ) internal returns (uint256 _limit, uint256 _lastBorrow) {
        (, uint256 _collateralFactor, ) = comptroller.markets(_bathToken);
        // how much is needed to borrow in asset form
        uint256 _desiredAmount = wmul(_assetAmount, _leverage);

        // 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
                    );
                } else {
                    _loopBorrowed = wmul(_assetAmount, _collateralFactor);
                }
            } else {
                _loopBorrowed = wmul(_loopBorrowed, _collateralFactor);
            }

            // here _assetAmount refers to the
            // TOTAL asset amount in the position
            _assetAmount += _loopBorrowed;

            if (_assetAmount > _desiredAmount) {
                // in case we've borrowed more than needed
                // return excess and calculate how much is
                // needed to borrow on the last loop
                // to not overflow _desiredAmount
                uint256 _borrowDelta = _desiredAmount.sub(
                    _assetAmount.sub(_loopBorrowed)
                );
                _lastBorrow = _borrowDelta.mul(WAD).div(_loopBorrowed);

                _limit++;
                break;
            } else if (_assetAmount == _desiredAmount) {
                // 1x short or perfect matching
                _limit++;
                break;
            } else {
                // default case
                _limit++;
            }
        }
    }

Inside the while loop there is one interesting thing: if (_minted != 0). If this true, that means that the user currently has some amount of _bathToken, which means that they already have some collateral that can be used to borrow on it. The _maxBorrow(_bathToken) function is called in order to know how many collateral user has and what is the max amount they can borrow using that collateral.

But in this case, if this if (_minted != 0) is not true, it means that the user currently doesn’t have any _bathToken, then _maxBorrow(_bathToken) will not be called. However, the user still can have collateral other than _bathToken token. In this case, that collateral also should be used to borrow on it, but it’s ignored.

Scenario: 1.The user opened position using bathUSDC and they currently have some amount as collateral inside bathUSDC. 2.Then they open positions using bathETH and in this case their bathUSDC collateral is not used.

Tools Used

VsCode

Recommended Mitigation Steps

You should check if the user has liquidity (uint256 _err, uint256 _liq, uint256 _shortfall) = comptroller.getAccountLiquidity(address(this)); In this case if _liq is not 0, then you should call _maxBorrow(_bathToken) and use that collateral.

daoio (Rubicon) confirmed


The original report as published on Code4rena, by rvierdiiev. 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 check only inspects the balance of the single _bathToken being borrowed against, rather than the account's overall liquidity across all markets.

Impact

The leverage loop may borrow less than the user's full borrowing capacity allows, so positions opened with existing collateral in other markets are smaller than intended.

What the attacker needed

Existing collateral in another bathToken; ability to open a leveraged position

The fix

The calculation should query the comptroller for the account's overall liquidity and, when liquidity is non-zero, include the maximum borrowable amount from existing collateral.

How it was found

  1. The user opens a position using bathUSDC and holds some collateral in bathUSDC.
  2. The user then opens a position using bathETH.
  3. The bathUSDC collateral is not counted when calculating the borrow limit for the new position.

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.

A lending pool lets you borrow against things you have deposited as security (collateral). This code works out how much you can borrow, but it only looks at security deposited in one particular pool token and forgets security you hold in other pool tokens. So someone with plenty of security elsewhere is treated as if they had none, and the tool borrows less than it 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.

More by rvierdiiev

View all

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