Skip to content
Business logic Audit finding Intermediate

_handleRepay of MoneyMarketHook does not consider the actual debt shares of the posId inside the position manager and could lead to a user’s tokens getting stuck inside the hook

The MoneyMarketHook contract calculates the repayment amount based on user-provided debt shares without checking the actual debt shares of the position, so if a liquidator front-runs and reduces the debt, the transferred tokens exceed what is needed and the excess becomes stuck in the hook. An attacker could exploit this by front-running a user's repayment with a liquidation, causing the user's tokens to be locked in the contract.

said Follow

Originally disclosed on Code4rena

Submitted by said

When users construct repay operations via MoneyMarketHook, it doesn’t consider the actual debt shares of the position inside the InitCore and PosManager. This could lead to users’ tokens getting stuck inside the MoneyMarketHook contract.

Proof of Concept

When users want to repay his positions in MoneyMarketHook, they can provide the parameters inside repayParams, and MoneyMarketHook will construct the operation via _handleRepay function.

https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/hook/MoneyMarketHook.sol#L145-L159

    function _handleRepay(uint _offset, bytes[] memory _data, uint _initPosId, RepayParams[] memory _params)
        internal
        returns (uint, bytes[] memory)
    {
        for (uint i; i < _params.length; i = i.uinc()) {
            address uToken = ILendingPool(_params[i].pool).underlyingToken();
>>>         uint repayAmt = ILendingPool(_params[i].pool).debtShareToAmtCurrent(_params[i].shares);
            _ensureApprove(uToken, repayAmt);
>>>         IERC20(uToken).safeTransferFrom(msg.sender, address(this), repayAmt);
            _data[_offset] =
                abi.encodeWithSelector(IInitCore.repay.selector, _params[i].pool, _params[i].shares, _initPosId);
            _offset = _offset.uinc();
        }
        return (_offset, _data);
    }

It can be observed that it calculates the repayAmt based on the shares provided by the users and transfers the corresponding amount of tokens from the sender to the hook. However, the actual debt shares of the position can be less than the _params[i].shares provided by users. This means that the actual repay amount of tokens needed could be less than the calculated repayAmt.

https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L530-L551

    function _repay(IConfig _config, uint16 _mode, uint _posId, address _pool, uint _shares)
        internal
        returns (address tokenToRepay, uint amt)
    {
        // check status
        _require(_config.getPoolConfig(_pool).canRepay && _config.getModeStatus(_mode).canRepay, Errors.REPAY_PAUSED);
        // get position debt share
>>>     uint positionDebtShares = IPosManager(POS_MANAGER).getPosDebtShares(_posId, _pool);
>>>     uint sharesToRepay = _shares < positionDebtShares ? _shares : positionDebtShares;
        // get amtToRepay (accrue interest)
>>>     uint amtToRepay = ILendingPool(_pool).debtShareToAmtCurrent(sharesToRepay);
        // take token from msg.sender to pool
        tokenToRepay = ILendingPool(_pool).underlyingToken();
>>>     IERC20(tokenToRepay).safeTransferFrom(msg.sender, _pool, amtToRepay);
        // update debt on the position
        IPosManager(POS_MANAGER).updatePosDebtShares(_posId, _pool, -sharesToRepay.toInt256());
        // call repay on the pool
        amt = ILendingPool(_pool).repay(sharesToRepay);
        // update debt on mode
        IRiskManager(riskManager).updateModeDebtShares(_mode, _pool, -sharesToRepay.toInt256());
        emit Repay(_pool, _posId, msg.sender, _shares, amt);
    }

Consider a scenario where the user’s positions are currently liquidatable, and the user wishes to repay all of the position’s debt inside the MoneyMarketHook. However, a liquidator front-runs the operation by liquidating the user’s position. Now, when the repayment operation executes from MoneyMarketHook, it transfers the repayAmt to the MoneyMarketHook but the amount is not used/fully utilized and becomes stuck inside the contract.

Recommended Mitigation Steps

Consider to also check the provided shares against the actual debt shares inside the InitCore/PosManager.

fez-init (INIT) confirmed, but disagreed with severity and commented:

The issue should be medium, since the funds cannot be retrieved by someone else. The hook will be upgradeable, so if funds actually get stuck, it is still retrievable.

hansfriese (Judge) commented:

I agree that this issue is in the middle of Medium and High. Users might face a temporary lock on their funds, and the hook should be upgraded every time to unlock them.

Given the high probability of this scenario occurring, I will keep this issue as a valid High.


The original report as published on Code4rena, by said. 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 hook calculates the repayment amount from user-supplied shares without verifying against the actual debt shares held by the position manager, so any reduction in debt (e.g., via liquidation) causes an over-transfer that remains in the hook.

Impact

Users' tokens could become temporarily locked in the hook, requiring a contract upgrade to retrieve them; the funds are not at risk of theft by others.

What the attacker needed

User has a position in MoneyMarketHook; position is liquidatable; liquidator front-runs repayment

The fix

The hook should check the provided shares against the actual debt shares in InitCore/PosManager before transferring tokens.

How it was found

  1. User initiates a repayment via MoneyMarketHook with shares equal to the full debt.
  2. A liquidator front-runs and liquidates the position, reducing the actual debt shares.
  3. The repayment executes: the hook transfers the full repayAmt from the user to itself.
  4. InitCore repays only the remaining debt shares, leaving excess tokens stuck in the hook.

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.

When you repay a loan, you tell the contract how many 'shares' of debt you want to clear. The contract trusts your number and takes that much money from you. But if someone else (like a liquidator) has already reduced your debt, the contract takes too much money and the extra gets stuck in the contract, not returned to you.

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 said

View all

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