`LiquidationLogic@_burnCollateralTokens` does not account for liquidation fees when withdrawing collateral during liquidation leading to incorrect accounting and Pools insolvency
The liquidation logic burns collateral shares based only on the amount sent to the liquidator, ignoring the protocol fee that is also transferred out of the pool. This leaves the pool and the liquidated user with more supply shares than they should have, allowing the user to withdraw funds that were already paid as fees and potentially making the pool insolvent.
000000 Follow with A2-security, Bigsam and 8 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/228
Found by
000000, A2-security, Bigsam, Honour, dhank, ether_sky, imsrybr0, lemonmon, thisvishalsingh, trachev, zarkk01
Summary
LiquidationLogic@_burnCollateralTokens does not account for liquidation fees when withdrawing collateral during liquidation leading to incorrect accounting and Pools insolvency, ultimately impacting regular flows (.e.g borrows, withdrawals, redemptions, ...) in the protocol for the different actors (.i.e Pools users, Curated Vaults and their users, NFT Positions users).
Root Cause
// ...
library LiquidationLogic {
// ...
function executeLiquidationCall(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(address => mapping(bytes32 => DataTypes.PositionBalance)) storage balances,
mapping(address => DataTypes.ReserveSupplies) storage totalSupplies,
mapping(bytes32 => DataTypes.UserConfigurationMap) storage usersConfig,
DataTypes.ExecuteLiquidationCallParams memory params
) external {
// ...
(vars.actualCollateralToLiquidate, vars.actualDebtToLiquidate, vars.liquidationProtocolFeeAmount) = // <==== Audit
_calculateAvailableCollateralToLiquidate(
collateralReserve,
vars.debtReserveCache,
vars.actualDebtToLiquidate,
vars.userCollateralBalance,
vars.liquidationBonus,
IPool(params.pool).getAssetPrice(params.collateralAsset),
IPool(params.pool).getAssetPrice(params.debtAsset),
IPool(params.pool).factory().liquidationProtocolFeePercentage()
);
// ...
_burnCollateralTokens(
collateralReserve, params, vars, balances[params.collateralAsset][params.position], totalSupplies[params.collateralAsset]
); // <==== Audit
if (vars.liquidationProtocolFeeAmount != 0) {
// ...
IERC20(params.collateralAsset).safeTransfer(IPool(params.pool).factory().treasury(), vars.liquidationProtocolFeeAmount); // <==== Audit
}
// ...
}
function _burnCollateralTokens(
DataTypes.ReserveData storage collateralReserve,
DataTypes.ExecuteLiquidationCallParams memory params,
LiquidationCallLocalVars memory vars,
DataTypes.PositionBalance storage balances,
DataTypes.ReserveSupplies storage totalSupplies
) internal {
// ...
balances.withdrawCollateral(totalSupplies, vars.actualCollateralToLiquidate, collateralReserveCache.nextLiquidityIndex); // <==== Audit : actualCollateralToLiquidate doesn't include liquidation fees
IERC20(params.collateralAsset).safeTransfer(msg.sender, vars.actualCollateralToLiquidate);
}
// ...
function _calculateAvailableCollateralToLiquidate(
DataTypes.ReserveData storage collateralReserve,
DataTypes.ReserveCache memory debtReserveCache,
uint256 debtToCover,
uint256 userCollateralBalance,
uint256 liquidationBonus,
uint256 collateralPrice,
uint256 debtAssetPrice,
uint256 liquidationProtocolFeePercentage
) internal view returns (uint256, uint256, uint256) {
// ...
if (liquidationProtocolFeePercentage != 0) {
vars.bonusCollateral = vars.collateralAmount - vars.collateralAmount.percentDiv(liquidationBonus);
vars.liquidationProtocolFee = vars.bonusCollateral.percentMul(liquidationProtocolFeePercentage);
return (vars.collateralAmount - vars.liquidationProtocolFee, vars.debtAmountNeeded, vars.liquidationProtocolFee); // <==== Audit
} else {
return (vars.collateralAmount, vars.debtAmountNeeded, 0);
}
}
}
function withdrawCollateral(
DataTypes.PositionBalance storage self,
DataTypes.ReserveSupplies storage supply,
uint256 amount,
uint128 index
) internal returns (uint256 sharesBurnt) {
sharesBurnt = amount.rayDiv(index);
require(sharesBurnt != 0, PoolErrorsLib.INVALID_BURN_AMOUNT);
self.lastSupplyLiquidtyIndex = index;
self.supplyShares -= sharesBurnt; // <==== Audit
supply.supplyShares -= sharesBurnt; // <==== Audit
}
When there are protocol liquidation fees, _burnCollateralTokens doesn't account for liquidation fees when withrawing the collateral, leading to the pool and actor having more supply shares than reality.
Internal pre-conditions
Protocol liquidations fees are set.
External pre-conditions
N/A
Attack Path
Not an attack path per say as this happens in every liquidation when there are liquidation fees.
- Alice supplies
tokenA - Bob supplies
tokenB - Alice borrows
tokenB - Alice becomes liquidatable
- Bob liquidates Alice
Impact
- Incorrect accounting : pool and actor supply shares are higher than reality, allowing a liquidated actor to borrow more than what they should really be able to for example.
- Pools insolvency : since the liquidation fees are transferred to the treasury from the pool but not reflected on the pool and actor supply shares, the actor can withdraw them again at the expense of other actors. This leads to the other actors not being able to fully withdraw their provided collateral and potentially disrupting functionality such as Curated Vaults reallocation where the withdrawn amount cannot be controlled.
PoC
Test
function testLiquidationWithFees() external {
poolFactory.setLiquidationProtcolFeePercentage(0.05e4);
_mintAndApprove(alice, tokenA, 3000 ether, address(pool));
_mintAndApprove(bob, tokenB, 5000 ether, address(pool));
vm.startPrank(bob);
pool.supplySimple(address(tokenB), bob, 3000 ether, 0);
vm.stopPrank();
vm.startPrank(alice);
pool.supplySimple(address(tokenA), alice, 1000 ether, 0);
pool.borrowSimple(address(tokenB), alice, 375 ether, 0);
vm.stopPrank();
oracleB.updateAnswer(2.5e8);
vm.prank(bob);
pool.liquidateSimple(address(tokenA), address(tokenB), pos, type(uint256).max);
assertEq(pool.getBalance(address(tokenA), alice, 0), tokenA.balanceOf(address(pool)));
}
Results
forge test --match-test testLiquidationWithFees
[⠢] Compiling...
No files changed, compilation skipped
Ran 1 test for test/forge/core/pool/PoolLiquidationTests.t.sol:PoolLiquidationTest
[FAIL. Reason: assertion failed: 17968750000000000000 != 15625000000000000000] testLiquidationWithFees() (gas: 1003975)
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 5.25ms (1.62ms CPU time)
Ran 1 test suite in 347.96ms (5.25ms CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/forge/core/pool/PoolLiquidationTests.t.sol:PoolLiquidationTest
[FAIL. Reason: assertion failed: 17968750000000000000 != 15625000000000000000] testLiquidationWithFees() (gas: 1003975)
Encountered a total of 1 failing tests, 0 tests succeeded
Mitigation
diff --git a/zerolend-one/contracts/core/pool/logic/LiquidationLogic.sol b/zerolend-one/contracts/core/pool/logic/LiquidationLogic.sol
index e89d626..0a48da6 100644
--- a/zerolend-one/contracts/core/pool/logic/LiquidationLogic.sol
+++ b/zerolend-one/contracts/core/pool/logic/LiquidationLogic.sol
@@ -225,7 +225,7 @@ library LiquidationLogic {
);
// Burn the equivalent amount of aToken, sending the underlying to the liquidator
- balances.withdrawCollateral(totalSupplies, vars.actualCollateralToLiquidate, collateralReserveCache.nextLiquidityIndex);
+ balances.withdrawCollateral(totalSupplies, vars.actualCollateralToLiquidate + vars.liquidationProtocolFeeAmount, collateralReserveCache.nextLiquidityIndex);
IERC20(params.collateralAsset).safeTransfer(msg.sender, vars.actualCollateralToLiquidate);
}
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
The function that burns collateral shares uses only the amount sent to the liquidator, not the total amount removed from the user including the protocol fee.
Impact
The pool and the liquidated user have more supply shares than they should, allowing the user to borrow or withdraw more than entitled and potentially making the pool insolvent for other users.
What the attacker needed
Protocol liquidation fees are set
The fix
The withdrawal should burn shares for the sum of the amount sent to the liquidator and the protocol fee.
How it was found
- Set a liquidation protocol fee percentage.
- Alice supplies tokenA and Bob supplies tokenB.
- Alice borrows tokenB and becomes liquidatable.
- Bob liquidates Alice.
- The test checks that the pool's balance of tokenA equals Alice's recorded balance, but they differ.
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 is liquidated, part of their collateral is taken as a fee for the protocol. The code removes the collateral from the borrower's account but forgets to also remove the fee portion, so the borrower's balance stays too high. Later, the borrower can withdraw that extra amount, taking money that belongs to other users.
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.