Skip to content
Business logic Audit finding Intermediate

LeverageManager remove leverage will lead to stuck tokens if slippage `_podSwapAmtOutMin` is set

When removing leverage, if the user sets a minimum output amount for the pTKN to borrowed token swap, any leftover borrowed tokens are not returned to the user but remain stuck in the contract. An attacker could potentially exploit this by front-running to cause a larger leftover, but the report does not describe an attack path.

X77 Follow with ZoA, elolpuer and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/446

Found by

X77, ZoA, elolpuer, pkqs90

Summary

LeverageManager remove leverage will lead to stuck tokens if slippage _podSwapAmtOutMin is set.

Root Cause

First we need to understand the workflow of removeLeverage in LeverageManager.

  1. Borrow _borrowAssetAmt underlying token from flashloan source.
  2. Repay these tokens to FraxlendPair.
  3. Withdraw collateral (aspTKN) to borrowedTKN and pTKN. If borrowedTKN is not enough to repay the flashloan, swap pTKN to borrowedTKN.
  4. Repay flashloan.
  5. Send leftover borrowedTKN and pTKN back to position owner.

In step 3, it conducts an exactOutput swap. The target amount of output is the required amount of borrowedTKN for repay. However, this is susceptible to frontrunning and sandwich attacks. So the user needs to explicitly set a _podSwapAmtOutMin parameter as slippage.

The issue here is, if _podSwapAmtOutMin is set, and there are leftover borrowTKNs, they are not transferred to the user, but stuck in the contract. This is because _borrowAmtRemaining will always be zero in this case.

For example, after redeeming the spTKN, we have 100 pTKN and 100 borrowedTKN (pTKN:borrowedTKN = 1:1). However, we need to repay 120 borrowedTKN. If user don't set _podSwapAmtOutMin, the pTKN -> borrowedTKN swap would be a maxInput=100, exactOutput=20 swap, which can obviously be sandwiched. If user sets _podSwapAmtOutMin to 95 for slippage, the remaining 95-20=75 borrowedTokens are not transferred back to user.

https://github.com/sherlock-audit/2025-01-peapods-finance/blob/main/contracts/contracts/lvf/LeverageManager.sol#L1

Solidity
    function _swapPodForBorrowToken(
        address _pod,
        address _targetToken,
        uint256 _podAmt,
        uint256 _targetNeededAmt,
        uint256 _podSwapAmtOutMin
    ) internal returns (uint256 _podRemainingAmt) {
        IDexAdapter _dexAdapter = IDecentralizedIndex(_pod).DEX_HANDLER();
        uint256 _balBefore = IERC20(_pod).balanceOf(address(this));
        IERC20(_pod).safeIncreaseAllowance(address(_dexAdapter), _podAmt);
@>      _dexAdapter.swapV2SingleExactOut(
            _pod, _targetToken, _podAmt, _podSwapAmtOutMin == 0 ? _targetNeededAmt : _podSwapAmtOutMin, address(this)
        );
        _podRemainingAmt = _podAmt - (_balBefore - IERC20(_pod).balanceOf(address(this)));
    }

    function _removeLeveragePostCallback(bytes memory _userData)
        internal
        returns (uint256 _podAmtRemaining, uint256 _borrowAmtRemaining)
    {
        ...
        // pay back flash loan and send remaining to borrower
        uint256 _repayAmount = _d.amount + _d.fee;
        if (_pairedAmtReceived < _repayAmount) {
            _podAmtRemaining = _acquireBorrowTokenForRepayment(
                _props,
                _posProps.pod,
                _d.token,
                _repayAmount - _pairedAmtReceived,
                _podAmtReceived,
                _podSwapAmtOutMin,
                _userProvidedDebtAmtMax
            );
        }
        IERC20(_d.token).safeTransfer(IFlashLoanSource(_getFlashSource(_props.positionId)).source(), _repayAmount);
@>      _borrowAmtRemaining = _pairedAmtReceived > _repayAmount ? _pairedAmtReceived - _repayAmount : 0;
        emit RemoveLeverage(_props.positionId, _props.owner, _collateralAssetRemoveAmt);
    }

Internal pre-conditions

  • When removing leverage, a pTKN -> borrowTKN swap is required, and user sets _podSwapAmtOutMin for slippage.

External pre-conditions

N/A

Attack Path

N/A

Impact

Leftover borrowedTokens are locked in the contract.

PoC

N/A

Mitigation

Also transfer the remaining borrowTKN to user.

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

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 contract calculates the remaining borrowed tokens based only on the amount received before the swap, ignoring any leftover from the swap when a minimum output is set.

Impact

Leftover borrowed tokens are locked in the contract, and the user loses them.

What the attacker needed

Removing leverage; pTKN to borrowTKN swap needed; user sets _podSwapAmtOutMin

The fix

The contract should also transfer any remaining borrowed tokens to the user after repaying the flash loan.

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 remove leverage, the contract sometimes swaps one token for another to repay a loan. If you set a minimum amount to receive from that swap to protect against price manipulation, the contract may end up with extra borrowed tokens that it does not send back to you, leaving them stuck. This means you could lose funds.

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 X77

View all

6 more disclosed reports, best first.

More from Peapods

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.