Invalid handling of flash loan fees in PositionAction::onCreditFlashLoan , forcing it to always revert
The flash loan callback in PositionAction used the full borrowed amount to repay a position's debt without reserving the flash loan fee, so the subsequent repayment transfer exceeded the contract's balance. With any non-zero fee, decreaseLever would always revert, blocking users from reducing leverage.
Submitted by 0xAlix2, also found by pkqs90 (1, 2)
When users take a flash loan, an amount is sent to the receiver, and then some action takes place, after that action that sent amount is expected to be paid and some fee. Users can also call PositionAction::decreaseLever through a proxy, to “Decrease the leverage of a position by taking out a credit flash loan to withdraw and sell collateral”, after it is called, the flash loan lender sends the credit and calls onCreditFlashLoan, which handles all that logic.
This was reported in Issue 524, and a fix has been implemented. However, the fix is incomplete, and the onCreditFlashLoan will always revert when the fees are >0.
The fix added includes adding fees to the approval amounts:
underlyingToken.forceApprove(address(leverParams.vault), subDebt + fee);
underlyingToken.forceApprove(address(flashlender), subDebt + fee);
That fix still misses a point; the amount coming from the flash lender is constant, and that amount will be used to repay a position’s debt. The issue here is that all the amount is being used to repay without accounting for the extra fees that the flash lender will be requesting.
This causes PositionAction::onCreditFlashLoan to always revert.
Proof of Concept
Add the following test in src/test/integration/PositionAction20.lever.t.sol:
function test_leverageDownNotAccountingFees() public {
// Re-initialize the system to have fees > 0
flashlender = new Flashlender(IPoolV3(address(liquidityPool)), 0.01 ether);
liquidityPool.setCreditManagerDebtLimit(address(flashlender), type(uint256).max);
positionAction = new PositionAction20(
address(flashlender),
address(swapAction),
address(poolAction),
address(vaultRegistry)
);
uint256 depositAmount = 1_000 ether;
uint256 borrowAmount = 200 ether;
deal(address(token), user, depositAmount);
address[] memory assets = new address[](2);
assets[0] = address(underlyingToken);
assets[1] = address(token);
vm.startPrank(user);
// User deposits 1k ETH collateral
token.approve(address(vault), type(uint256).max);
vault.deposit(address(userProxy), depositAmount);
// User borrows 200 ETH
userProxy.execute(
address(positionAction),
abi.encodeWithSelector(
positionAction.borrow.selector,
address(userProxy),
address(vault),
CreditParams({amount: borrowAmount, creditor: user, auxSwap: emptySwap})
)
);
vm.expectRevert(bytes("ERC20: transfer amount exceeds balance"));
userProxy.execute(
address(positionAction),
abi.encodeWithSelector(
positionAction.decreaseLever.selector,
LeverParams({
position: address(userProxy),
vault: address(vault),
collateralToken: address(token),
primarySwap: SwapParams({
swapProtocol: SwapProtocol.BALANCER,
swapType: SwapType.EXACT_OUT,
assetIn: address(token),
amount: 1 ether,
limit: 2 ether,
recipient: address(positionAction),
residualRecipient: address(positionAction),
deadline: block.timestamp,
args: abi.encode(weightedPoolIdArray, assets)
}),
auxSwap: emptySwap,
auxAction: emptyPoolActionParams
}),
2 ether,
address(userProxy)
)
);
vm.stopPrank();
}
Recommended Mitigation Steps
function onCreditFlashLoan(
address /*initiator*/,
uint256 /*amount*/,
uint256 fee,
bytes calldata data
) external returns (bytes32) {
...
// sub collateral and debt
ICDPVault(leverParams.vault).modifyCollateralAndDebt(
leverParams.position,
address(this),
address(this),
0,
- -toInt256(subDebt)
+ -toInt256(subDebt - fee)
);
...
return CALLBACK_SUCCESS_CREDIT;
}
Assessed type
DoS
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 callback subtracts the full subDebt from the position instead of subDebt minus the flash loan fee, leaving no balance to cover the fee.
Impact
With any non-zero flash loan fee, decreaseLever always reverts, so users cannot reduce their leverage; the text does not claim any loss of funds.
What the attacker needed
Flash loan fee greater than zero; user has an open leveraged position
The fix
The callback should subtract the fee from the debt amount it repays, so the fee stays available to pay the lender.
How it was found
- Deploy a flash lender configured with a non-zero fee and point the position action at it.
- Deposit collateral into the vault and borrow against it through the user proxy.
- Call decreaseLever through the proxy so the flash loan callback runs.
- Observe the transaction revert because the repayment transfer exceeds the contract balance.
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 flash loan is a loan that must be repaid, plus a small fee, within the same transaction. Here the code spent the whole borrowed amount on repaying debt and forgot to keep enough back for the fee, so the final transfer failed and the whole action was cancelled.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.