PositionAction.sol#onCreditFlashLoan may have leftover tokens after conducting leverParams.auxSwap
When decreasing leverage with an auxiliary swap enabled, the contract performs an exact-input swap using a hardcoded input amount, so any collateral left over after the primary swap is not fully consumed. Those leftover collateral tokens remain stuck in the contract instead of being returned to the recipient.
Submitted by pkqs90, also found by pkqs90, lian886, Bauchibred, 0xbepresent, and hash (1, 2)
First, let’s inspect how deposit decreaseLever with swap enabled works:
- Borrow loans from flashLender.
- Repays CDPVault debt with the borrowed loans.
- Withdraws collateral from CDPVault.
- Conducts a
leverParams.primarySwapand swap collateral to debt token.
Now, step 4 is an EXACT_OUT swap, since it is forced to swap the exact amount of debt tokens used to repay the flashloan. However, after step 4, there may be some collateral tokens left, which is the residualAmount.
If leverParams.auxSwap is not enabled, the collateral token is simply sent back to the recipient. However, if leverParams.auxSwap is enabled, a swap is performed.
The issue here is, the leverParams.auxSwap swap is an EXACT_IN swap, and user would hardcode the amount of inTokens used for this swap. There is no way to know the exact amount of collateral tokens left after step 4, so there must still be some collateral tokens leftover after the leverParams.auxSwap.
These leftover tokens are not sent to anybody, and stuck in the contract.
function decreaseLever(
LeverParams calldata leverParams,
uint256 subCollateral,
address residualRecipient
) external onlyDelegatecall {
// validate the primary swap
if (leverParams.primarySwap.swapType != SwapType.EXACT_OUT || leverParams.primarySwap.recipient != self)
revert PositionAction__decreaseLever_invalidPrimarySwap();
// validate aux swap if it exists
> if (leverParams.auxSwap.assetIn != address(0) && (leverParams.auxSwap.swapType != SwapType.EXACT_IN))
revert PositionAction__decreaseLever_invalidAuxSwap();
...
}
function onCreditFlashLoan(
address /*initiator*/,
uint256 /*amount*/,
uint256 /*fee*/,
bytes calldata data
) external returns (bytes32) {
if (msg.sender != address(flashlender)) revert PositionAction__onCreditFlashLoan__invalidSender();
(
LeverParams memory leverParams,
uint256 subCollateral,
address residualRecipient
) = abi.decode(data,(LeverParams, uint256, address));
uint256 subDebt = leverParams.primarySwap.amount;
underlyingToken.forceApprove(address(leverParams.vault), subDebt);
// sub collateral and debt
ICDPVault(leverParams.vault).modifyCollateralAndDebt(
leverParams.position,
address(this),
address(this),
0,
-toInt256(subDebt)
);
// withdraw collateral and handle any CDP specific actions
uint256 withdrawnCollateral = _onDecreaseLever(leverParams, subCollateral);
bytes memory swapData = _delegateCall(
address(swapAction),
abi.encodeWithSelector(
swapAction.swap.selector,
leverParams.primarySwap
)
);
uint256 swapAmountIn = abi.decode(swapData, (uint256));
// swap collateral to stablecoin and calculate the amount leftover
uint256 residualAmount = withdrawnCollateral - swapAmountIn;
// send left over collateral that was not needed to payback the flash loan to `residualRecipient`
if (residualAmount > 0) {
// perform swap from collateral to arbitrary token if necessary
> if (leverParams.auxSwap.assetIn != address(0)) {
_delegateCall(
address(swapAction),
abi.encodeWithSelector(
swapAction.swap.selector,
leverParams.auxSwap
)
);
} else {
// otherwise just send the collateral to `residualRecipient`
IERC20(leverParams.primarySwap.assetIn).safeTransfer(residualRecipient, residualAmount);
}
}
underlyingToken.forceApprove(address(flashlender), subDebt);
return CALLBACK_SUCCESS_CREDIT;
}
Recommended Mitigation Steps
Send the amount of IERC20(leverParams.primarySwap.assetIn).balance(address(this)) to residualRecipient to make sure there are no leftovers.
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 auxiliary swap is forced to be exact-input with a user-supplied amount, so it cannot consume the unpredictable residual collateral balance.
What the attacker needed
AuxSwap enabled; decreaseLever with swap
The fix
The contract should transfer its full remaining balance of the input token to the residual recipient after the auxiliary swap.
How it was found
- Borrow from the flash lender and repay part of the vault debt.
- Withdraw collateral from the vault.
- Run the primary exact-output swap of collateral to the debt token.
- Run the auxiliary exact-input swap on the residual collateral.
- Observe that leftover collateral remains in the contract.
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 user can reduce their loan by swapping some collateral back into the borrowed token. If they also ask for a second swap of the leftover collateral, the code swaps a fixed amount rather than whatever is actually left, so a small amount of collateral is never moved and stays locked in the contract.
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.