Incorrect liquidation reward computation causes excess liquidator rewards to be given
The liquidator reward was calculated using the user's borrow part without accumulated interest, so the reward was higher than it should be. An attacker could liquidate a position and receive excess rewards, shortchanging the protocol.
Submitted by peakbolt, also found by minhtrng, bin2chen, carrotsmuggler, 0x007, and 0xRobocop (1, 2)
In _liquidateUser() for BigBang and Singularity, the liquidator reward is derived by _getCallerReward(). However, it is incorrectly computed using userBorrowPart[user], which is the portion of borrowed amount that does not include the accumulated fees (interests).
uint256 callerReward = _getCallerReward(
//@audit - userBorrowPart[user] is incorrect as it does not include accumulated fees
userBorrowPart[user],
startTVLInAsset,
maxTVLInAsset
);
Using only userBorrowPart[user] is inconsistent with liquidation calculation in Market.sol#L423-L424, which is based on borrowed amount including accumulated fees.
function _isSolvent(
address user,
uint256 _exchangeRate
) internal view returns (bool) {
...
return
yieldBox.toAmount(
collateralId,
collateralShare *
(EXCHANGE_RATE_PRECISION / FEE_PRECISION) *
collateralizationRate,
false
) >=
//@audit - note that the collateralizion calculation is based on borrowed amount with fees (using totalBorrow.elastic)
// Moved exchangeRate here instead of dividing the other side to preserve more precision
(borrowPart * _totalBorrow.elastic * _exchangeRate) /
_totalBorrow.base;
}
As the protocol uses a dynamic liquidation incentives mechanism (see below), the liquidator will be given more rewards than required if the liquidator reward is derived by borrowed amount without accumulated fees. That is because the dynamic liquidation incentives mechanism decreases the rewards as it reaches 100% LTV. So computing the liquidator rewards using a lower value (without fees) actually gives liquidator a higher portion of the rewards.
function _getCallerReward(
uint256 borrowed,
uint256 startTVLInAsset,
uint256 maxTVLInAsset
) internal view returns (uint256) {
if (borrowed == 0) return 0;
if (startTVLInAsset == 0) return 0;
if (borrowed < startTVLInAsset) return 0;
if (borrowed >= maxTVLInAsset) return minLiquidatorReward;
uint256 rewardPercentage = ((borrowed - startTVLInAsset) *
FEE_PRECISION) / (maxTVLInAsset - startTVLInAsset);
int256 diff = int256(minLiquidatorReward) - int256(maxLiquidatorReward);
int256 reward = (diff * int256(rewardPercentage)) /
int256(FEE_PRECISION) +
int256(maxLiquidatorReward);
return uint256(reward);
}
Impact
The protocol is shortchanged as it gives liquidator more rewards than required.
Proof of Concept
- Add the following console.log to BigBang.sol#L581`
console.log(" callerReward (without fees) = \t %d (actual)", callerReward);
callerReward= _getCallerReward(
//userBorrowPart[user],
//@audit borrowed amount with fees
(userBorrowPart[user] * totalBorrow.elastic) / totalBorrow.base,
startTVLInAsset,
maxTVLInAsset
);
console.log(" callerReward (with fees) = \t %d (expected)", callerReward);
- Add and run the following test in
bigBang.test.ts. The console.log will show that the expected liquidator reward is lower when computed using borrowed amount with fees.
it.only('peakbolt - liquidation reward computation', async () => {
const {
wethBigBangMarket,
weth,
wethAssetId,
yieldBox,
deployer,
eoa1,
__wethUsdcPrice,
__usd0WethPrice,
multiSwapper,
usd0WethOracle,
timeTravel,
} = await loadFixture(register);
await weth.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox.setApprovalForAll(wethBigBangMarket.address, true);
await weth
.connect(eoa1)
.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox
.connect(eoa1)
.setApprovalForAll(wethBigBangMarket.address, true);
const wethMintVal = ethers.BigNumber.from((1e18).toString()).mul(
10,
);
await weth.connect(eoa1).freeMint(wethMintVal);
const valShare = await yieldBox.toShare(
wethAssetId,
wethMintVal,
false,
);
await yieldBox
.connect(eoa1)
.depositAsset(
wethAssetId,
eoa1.address,
eoa1.address,
0,
valShare,
);
console.log("wethMintVal = %d", wethMintVal);
console.log("__wethUsdcPrice = %d", __wethUsdcPrice);
console.log("--------------------- addCollateral ------------------------");
await wethBigBangMarket
.connect(eoa1)
.addCollateral(eoa1.address, eoa1.address, false, 0, valShare);
//borrow
const usdoBorrowVal = wethMintVal
.mul(74)
.div(100)
.mul(__wethUsdcPrice.div((1e18).toString()));
console.log("--------------------- borrow ------------------------");
await wethBigBangMarket
.connect(eoa1)
.borrow(eoa1.address, eoa1.address, usdoBorrowVal);
// Can't liquidate
const swapData = new ethers.utils.AbiCoder().encode(
['uint256'],
[1],
);
timeTravel(100 * 86400);
console.log("--------------------- price drop ------------------------");
const priceDrop = __usd0WethPrice.mul(15).div(10).div(100);
await usd0WethOracle.set(__usd0WethPrice.add(priceDrop));
await wethBigBangMarket.updateExchangeRate();
const borrowPart = await wethBigBangMarket.userBorrowPart(
eoa1.address,
);
console.log("--------------------- liquidate (success) ------------------------");
await expect(
wethBigBangMarket.liquidate(
[eoa1.address],
[borrowPart],
multiSwapper.address,
swapData,
),
).to.not.be.reverted;
return;
});
Recommended Mitigation Steps
Change BigBang.sol#L576-L580, SGLLiquidation.sol#L310-L314, Market.sol#L364 from
uint256 callerReward = _getCallerReward(
userBorrowPart[user],
startTVLInAsset,
maxTVLInAsset
);
to
uint256 callerReward = _getCallerReward(
(userBorrowPart[user] * totalBorrow.elastic) / totalBorrow.base,
startTVLInAsset,
maxTVLInAsset
);
0xRektora (Tapioca) confirmed via duplicate issue 89
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
Impact
The protocol could give liquidators more rewards than required, shortchanging the protocol.
What the attacker needed
A borrower with an outstanding loan; the borrower's position becomes liquidatable; a liquidator calls the liquidate function
How it was found
- Add a console log to BigBang.sol to print the caller reward computed with and without fees.
- Add a test in bigBang.test.ts that sets up a borrower, adds collateral, borrows, time travels, drops the price, and liquidates.
- Run the test and observe that the expected reward (with fees) is lower than the actual reward (without fees).
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.
In a lending protocol, when a borrower's collateral falls below a required level, a liquidator can repay part of the debt and earn a reward. The reward should be based on the total debt including interest, but here it was based only on the original borrowed amount without interest. Because the reward formula gives a higher percentage for lower debt values, this mistake made the reward larger than intended, so the liquidator got extra money at the protocol's expense.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.