Invalid handling of risdual amount in PositionAction::onCreditFlashLoan , forcing it to revert
The PositionAction contract's onCreditFlashLoan function mishandles residual swap amounts by attempting to repay extra debt, which can cause a revert when the user tries to repay their entire debt. An attacker could exploit this to cause a denial of service for users attempting to decrease leverage.
Users can 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. When doing so, users are supposed to swap their collateral withdrawn into debt tokens so that the flash loan can be repaid.
The protocol tries to handle the residual amount from the swap (swapped - paid debt), by trying to repay extra debt for the designated position, using:
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 2.65s (4.18ms CPU time)
Ran 1 test suite in 2.66s (2.65s CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in src/test/integration/PositionAction20.lever.t.sol:PositionAction20_Lever_Test
[FAIL. Reason: CallerNotCreditManagerException()] test_leverageDownWrongResidualHandling() (gas: 1088990)
Recommended Mitigation Steps
Rather than using the residual amount to repay excess debt (that might not even exist), transfer it to the designated residual recipient. Alternatively, check if the user still has any remaining debt. If they do, use the residual to repay it; otherwise, transfer the residual amount to the recipient.
The error in the failing test is because of a faulty setup. This will happen if the flashlender contract is not added to the poolv3 as a credit manager.
@Koolex - I believe there’s confusion here, the error here isn’t because “the flashlender contract is not added to the poolv3 as a credit manager”, please let me explain:
In PoolV3.sol, if you search for CallerNotCreditManagerException you can find 2 occurrences, the first is here:
function _revertIfCallerNotCreditManager() internal view {
if (!_creditManagerSet.contains(msg.sender)) {
revert CallerNotCreditManagerException(); // U:[PQK-4]
}
}
This is indeed the case that the sponsor is referencing; however, there’s another occurrence here:
if (cmBorrowed == 0) {
revert CallerNotCreditManagerException(); // U:[LP-2C,14A]
}
This is the case that the report is discussing, where this error is thrown when the borrowed of a position equals 0.
You can easily confirm this by changing the amount passed to amount in the above PoC to a lower value and see that the test doesn’t fail, example below:
@amarcu - I have run the PoC and changed the error:
if (cmBorrowed == 0) {
revert CallerNotCreditManagerException(); // U:[LP-2C,14A]
}
to other a different one. When running the PoC, it throws this error. This confirms the error is caused when there is no debt left cmBorrowed == 0.
The original report as published on Code4rena, by 0xAlix2. 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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The contract unconditionally attempts to repay residual debt without checking if any debt remains, leading to a revert when the debt is zero.
Impact
Users cannot fully repay their debt using decreaseLever, causing a denial of service for that function.
What the attacker needed
User has a leveraged position; User calls decreaseLever to repay full debt
The fix
The contract should transfer the residual amount to the designated residual recipient instead of attempting to repay debt, or check if debt remains before repaying.
How it was found
User deposits collateral and borrows debt.
User calls decreaseLever with primarySwap.amount equal to the full debt.
The flash loan executes and the swap produces a residual amount.
The contract tries to repay the residual amount as extra debt.
The transaction reverts because there is no debt left to repay.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you use a flash loan to reduce your leverage, you swap some collateral to get debt tokens. If you get more tokens than needed to repay the loan, the extra is called a residual. The contract tries to use that extra to repay more of your debt, but if you already repaid everything, it tries to repay debt that doesn't exist and the transaction fails. This means you can't fully close your position using this function.
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.