Liquidator might end up paying much more asset than collateral received
The liquidateClean function does not verify that the repaid shares are fully covered by the available collateral, so a liquidator can repay more than the collateral is worth and receive only the available collateral. This can cause the liquidator to lose funds, especially if a validator reorders transactions to front-run a well-calculated liquidation.
Submitted by 0xA5DF, also found by 141345
liquidateClean doesn’t check that the amount of shares the liquidator repays are covered by the collateral available, if the user repays more shares than (equally worth) collateral available - he’ll get only the amount of collateral available.
Impact
Liquidator might end up paying assets that are worth much more than the collateral he’s received.
Even though it’s also the liquidator’s responsibility to check that there’s enough collateral available, mistakes are commonly done (wrong calculation, typos, deadline too large) and the protocol should prevent those kinds of errors if possible.
Otherwise users might lose trust in the protocol when they lose their funds due to errors.
Proof of Concept
The following test was added to src/test/e2e/LiquidationPairTest.sol. In this scenario the user ends up with collateral that’s worth only 1/3 of the asset-tokens he paid.
function testCleanLiquidate() public {
// Setup contracts
defaultSetUp();
// Sets price to 3200 USD per ETH
uint256 _amountToBorrow = 16e20; // 1.5k
uint256 _amountInPool = 15e23; // 1.5m
// collateral is 1.5 times borrow amount
oracleDivide.setPrice(3200, 1, vm);
mineBlocks(1);
(, uint256 _exchangeRate) = pair.exchangeRateInfo();
uint256 _collateralAmount = (_amountToBorrow * _exchangeRate * 3) / (2 * 1e18);
faucetFunds(asset, _amountInPool);
faucetFunds(collateral, _collateralAmount);
lendTokenViaDeposit(_amountInPool, users[0]);
borrowToken(uint128(_amountToBorrow), _collateralAmount, users[2]);
uint256 mxltv = pair.maxLTV();
uint256 cleanLiquidationFee = pair.cleanLiquidationFee();
uint256 liquidation_price = ((((1e18 / _exchangeRate) * mxltv) / 1e5) * (1e5 + cleanLiquidationFee)) / 1e5;
liquidation_price /= 4; // Collateral price goes down enough so that it doesn't cover the loan
oracleDivide.setPrice(liquidation_price, 1, vm);
mineBlocks(1);
uint128 _shares = pair.userBorrowShares(users[2]).toUint128();
for (uint256 i = 0; i < 1; i++) {
pair.addInterest();
mineOneBlock();
}
vm.startPrank(users[1]);
pair.addInterest();
asset.approve(address(pair), toBorrowAmount(_shares, true));
uint256 liquidatorBalanceBefore = asset.balanceOf(users[1]);
uint256 collateralGained = pair.liquidateClean(_shares, block.timestamp, users[2]);
uint256 liquidatorBalanceAfter = asset.balanceOf(users[1]);
uint256 balanceDiff = liquidatorBalanceBefore - liquidatorBalanceAfter;
console2.log("Balance diff is: ", balanceDiff);
console2.log("_exchangeRate is: ", _exchangeRate);
console2.log("cleanLiquidationFee is: ", cleanLiquidationFee);
(, _exchangeRate) = pair.exchangeRateInfo();
// 11/10 is for liquidation fee, 1e18 is for exchange precision
assertEq(collateralGained, balanceDiff * _exchangeRate * 11 / 10 / 1e18);
assertEq(pair.userBorrowShares(users[2]), 0);
vm.stopPrank();
}
Output:
Balance diff is: 1600000011384589113999
_exchangeRate is: 863759326621800
cleanLiquidationFee is: 10000
Error: a == b not satisfied [uint]
Expected: 7394958035811125166
Actual: 2073022383892320000
Recommended Mitigation Steps
Check that the shares intended to be repaid are worth the collateral + fee, if not reduce the amount of shares to be repaid accordingly.
DrakeEvans (Frax) acknowledged, but disagreed with severity and commented:
This is by design, the deadline parameter can protect against this. Liquidators are encouraged to provide values such that all collateral is cleared. This does present the risk of slightly (a few wei) overpaying for the collateral. However, they are compensated with a high liquidation fee. They can protect themselves by inputting a deadline to the tx as well to prevent interest accruing and an old tx being replayed.
My concern is with transaction ordering. While the deadline can prevent stale transactions, nothing prevents a validator (or whoever orders transactions in the future) from inserting their repayment prior to a well calculated transaction. An MEV drain.
What protects the victim in this case is the _totalBorrow parameter. It will be lower and so place a lower bound on the overpayment.
Since liquidators can’t protect themselves from MEV, there is potential leakage. Leaving severity at Medium.
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 liquidateClean function lacks a check that the repaid shares are fully covered by the available collateral plus fee, allowing overpayment.
What the attacker needed
Liquidator calls liquidateClean; collateral value is insufficient; or transaction reordering by validator
The fix
The protocol should check that the shares intended to be repaid are worth the collateral plus fee, and if not, reduce the amount of shares to be repaid accordingly.
How it was found
- Set up the contracts and set the oracle price to 3200 USD per ETH.
- Borrow 16e20 asset tokens with collateral worth 1.5 times the borrow amount.
- Lower the collateral price so that it no longer covers the loan.
- As a liquidator, approve the asset tokens for repayment and call liquidateClean with the borrower's shares.
- Observe that the collateral gained is only about one third of the asset tokens paid.
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 a borrower's collateral falls below a required level, a liquidator can repay the borrower's debt and receive the collateral at a discount. This report shows that the function handling this repayment does not check that the collateral is enough to cover the repayment. So a liquidator can accidentally pay more than the collateral is worth and lose money.
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.