Skip to content
Business logic Audit finding Advanced

Admin configuration isAllowedForCollateral(mode, pool) can be bypassed by donating asset to the pool directly and then trigger sync cash via flashloan

The admin's restriction on which lending pools can be used as collateral can be bypassed by donating tokens directly to a pool and then triggering a cash sync via a flash loan. This inflates the pool's share value, allowing an attacker to borrow more than intended, especially if they can mint tokens infinitely.

ladboy233 Follow with rvierdiiev

Originally disclosed on Code4rena

Submitted by ladboy233, also found by rvierdiiev

In the current implementation, after user deposits funds into lending pool and mint lending pool shares, user can call collateralize function to add collateral:

    function collateralize(uint _posId, address _pool) public virtual onlyAuthorized(_posId) nonReentrant {
        IConfig _config = IConfig(config);
        // check mode status
        uint16 mode = _getPosMode(_posId);
        _require(_config.getModeStatus(mode).canCollateralize, Errors.COLLATERALIZE_PAUSED);
        // check if the position mode supports _pool
        _require(_config.isAllowedForCollateral(mode, _pool), Errors.INVALID_MODE);
        // update collateral on the position
        uint amtColl = IPosManager(POS_MANAGER).addCollateral(_posId, _pool);
        emit Collateralize(_posId, _pool, amtColl);
    }

There is a validation:

 _require(_config.isAllowedForCollateral(mode, _pool), Errors.INVALID_MODE);

Let us consider the case:

The mode supports three pools, USDC lending pool, WETH lending pool and a token A lending pool.

The token A is subject to high volatility, the admin decides to disalllow token A lending pool added as collateral.

But then the token A is hacked.

The attacker can mint the token A infinitely.

There is a relevant hack in the past:

  1. https://ciphertrace.com/infinite-minting-exploit-nets-attacker-4-4m/

Attacker exploited logical and math error to mint token infinitely.

  1. https://www.coindesk.com/business/2022/10/10/binance-exec-bnb-smart-chain-hack-could-have-been-worse-if-validators-hadnt-sprung-into-action/

Attacker exploited cryptographical logic to mint token infinitely.

But even when admin disallow a mode from further collaterize or disallow the lending pool from further collaterize, the hacker can donate the token to the lending pool and inflate the share worth to borrow all fund out.

  1. hacker transfers the infinitely minted token to the lending pool
  2. then hacker can call the function flash

This would trigger the function syncCash, which update the cash amount in the lending pool to make sure the donated token count into the token worth

// execute callback
IFlashReceiver(msg.sender).flashCallback(_pools, _amts, fees, _data);
// sync cash
for (uint i; i < _pools.length; i = i.uinc()) {
	uint poolCash = ILendingPool(_pools[i]).syncCash();

Sync cash is called

   /// @inheritdoc ILendingPool
    function syncCash() external accrue onlyCore returns (uint newCash) {
        newCash = IERC20(underlyingToken).balanceOf(address(this));
        _require(newCash >= cash, Errors.INVALID_AMOUNT_TO_REPAY); // flash not repay
        cash = newCash;
    }

Basically by using the flash function and then trigger syncCash, user can always donate the token to the pool to inflate share worth.

Then in the collateral credit calculation, we are converting shares worth to amount worth

            uint tokenValue_e36 = ILendingPool(pools[i]).toAmtCurrent(shares[i]) * tokenPrice_e36;

Which calls the function toAmt

   function _toAmt(uint _shares, uint _totalAssets, uint _totalShares) internal pure returns (uint amt) {
        return _shares.mulDiv(_totalAssets + VIRTUAL_ASSETS, _totalShares + VIRTUAL_SHARES);
    }
}

Assume shares do not change. Assume total shares does not change.

Clearly inflating the total asset (which is _cash + debt) inflates share worth.

Again, in the case of infinite token minting, hacker can donate the token to the lending pool to inflate the collateral credit and borrow all fund out and create large bad debt.

Recommended Mitigation Steps

Use internal balance to track the cash amount and do not allow user to indirectly access the sync cash function via flash loan.

fez-init (INIT) confirmed


The original report as published on Code4rena, by ladboy233. 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 pool's cash balance is updated by directly reading the token balance, so donations are counted as cash without minting shares, inflating share value.

Impact

An attacker could borrow all funds from the pool and create large bad debt, but this requires the ability to mint tokens infinitely.

What the attacker needed

Ability to mint tokens infinitely; attacker has some tokens to donate

The fix

Use an internal balance to track cash instead of the token balance, and prevent indirect access to syncCash via flash loans.

How it was found

  1. Transfer the infinitely minted token to the lending pool.
  2. Call the flash function to trigger syncCash.
  3. syncCash updates the cash amount to include the donated tokens.
  4. The inflated total assets increase the share value.
  5. Use the inflated collateral credit to borrow all funds.

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 people deposit tokens and get shares representing their deposit. The value of each share depends on the total tokens in the pool. If someone simply sends tokens to the pool without getting shares, the total goes up, so each share becomes worth more. The attacker uses a flash loan (a loan that must be repaid in the same transaction) to trigger an update that counts the donated tokens, then uses the inflated share value to borrow more than allowed.

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 ladboy233

View all

47 more disclosed reports, best first.

More from INIT Capital Invitational

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.