Incorrect calculation of newCumulativeIndex in function calcDecrease
The calcDecrease function in CDPVault.sol incorrectly calculates newCumulativeIndex when repaying an amount less than the total interest, because it uses profit that includes cumulativeQuotaInterest. This can cause the new index to exceed cumulativeIndexNow, leading to undercharged interest and positions that should be liquidated not being liquidated.
hearmen Follow with boraichodrunkenmaster, emerald7017 and 8 others
Originally disclosed on Code4rena
Submitted by hearmen, also found by boraichodrunkenmaster, emerald7017, chaduke, 0xBugSlayer, thisvishalsingh, joaovwfreire, 0xpiken, Chinmay, hash, and pkqs90
In the contract CDPVault.sol, the function calcDecrease calculates newCumulativeIndex in line 703 when amountToRepay < interestAccrued with profit which is:
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L703
newCumulativeIndex =
(INDEX_PRECISION * cumulativeIndexNow * cumulativeIndexLastUpdate) /
(INDEX_PRECISION *
cumulativeIndexNow -
(INDEX_PRECISION * profit * cumulativeIndexLastUpdate) /
debt); // U:[CL-3]
However, the profit contains the cumulativeQuotaInterest, so it can not be used to calculate newCumulativeIndex.
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L668
if (amountToRepay >= cumulativeQuotaInterest) {
amountToRepay -= cumulativeQuotaInterest; // U:[CL-3]
profit += cumulativeQuotaInterest; // U:[CL-3]
newCumulativeQuotaInterest = 0; // U:[CL-3]
}
For example, when the left amountToRepay can cover the interestAccrued, the newCumulativeIndex should be cumulativeIndexNow as in line 692, because: interestAccrued == (debt * cumulativeIndexNow) / cumulativeIndexLastUpdate - debt.
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L692
if (amountToRepay >= interestAccrued) {
amountToRepay -= interestAccrued;
profit += interestAccrued;
newCumulativeIndex = cumulativeIndexNow;
}
However, when the left amountToRepay = interestAccrued - 1, and profit will be cumulativeQuotaInterest+interestAccrued - 1. The calculation of newCumulativeIndex will be larger than cumulativeIndexNow because the cumulativeQuotaInterest+interestAccrued - 1 will be larger than interestAccrued which is totally wrong, since it exceeds the limit cumulativeIndexNow.
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L703
newCumulativeIndex =
(INDEX_PRECISION * cumulativeIndexNow * cumulativeIndexLastUpdate) /
(INDEX_PRECISION *
cumulativeIndexNow -
(INDEX_PRECISION * profit * cumulativeIndexLastUpdate) /
debt); // U:[CL-3]
Impact
Position.cumulativeIndexLastUpdate will be updated with incorrect newCumulativeIndex and less interest accrued will be charged from users. The position which should be liquidated will not be liquidated due the the wrong Position.cumulativeIndexLastUpdate.
Proof of Concept
Paste this test in CDPVault.t.sol:
function test_calcDecrese_poc() public {
//CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1.0 ether, 0);
CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1 ether, 0.95 ether);
createGaugeAndSetGauge(address(vault));
// create position
token.mint(address(this), 1000 ether);
token.approve(address(vault), 100 ether);
vault.modifyCollateralAndDebt(address(this), address(this), address(this), 100 ether, 70 ether);
vm.warp(block.timestamp + 865 days);
(, uint256 totalInterest, ) = vault.getDebtInfo(address(this));
mockWETH.mint(address(this), 500 ether);
mockWETH.approve(address(vault), 200 ether);
int256 repayAmount = -(int256(totalInterest - 1));
vault.modifyCollateralAndDebt(address(this), address(this), address(this), 0, repayAmount);
(,,,uint256 cumulativeIndexLastUpdate,, ) = vault.positions(address(this));
uint256 cumulativeIndexNow = liquidityPool.baseInterestIndex();
console.log("cumulativeIndexLastUpdate",cumulativeIndexLastUpdate);
console.log("cumulativeIndexNow",cumulativeIndexNow);
}
The poc result will be:
cumulativeIndexLastUpdate 1242341891861284810269427100cumulativeIndexNow 1239397711761394368218295007cumulativeIndexLastUpdate>cumulativeIndexNow
… which is wrong.
Recommended Mitigation Steps
Use the code as below:
else {
// If amount is not enough to repay interest, then send all to the stakers and update index
profit += amountToRepay; // U:[CL-3]
newCumulativeIndex =
(INDEX_PRECISION * cumulativeIndexNow * cumulativeIndexLastUpdate) /
(INDEX_PRECISION *
cumulativeIndexNow -
(INDEX_PRECISION * amountToRepay * cumulativeIndexLastUpdate) /
debt); // U:[CL-3]
amountToRepay = 0; // U:[CL-3]
}
Assessed type
Other
0xtj24 (LoopFi) confirmed and commented:
Fixed.
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 code uses the variable profit, which includes cumulativeQuotaInterest, in the formula for newCumulativeIndex instead of using only the amountToRepay that corresponds to interestAccrued.
Impact
Positions may be undercharged interest and not liquidated when they should be, due to an incorrect cumulativeIndexLastUpdate.
What the attacker needed
A CDPVault with quota interest; a position with debt; repayment less than total interest
How it was found
- Create a CDPVault with specific parameters.
- Create a position by depositing collateral and borrowing.
- Advance time by 865 days.
- Repay an amount equal to totalInterest - 1.
- Observe that cumulativeIndexLastUpdate is greater than cumulativeIndexNow.
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.
A smart contract calculates how much interest a borrower owes using a number called an index. When a borrower repays a small amount, the contract mistakenly uses a total that includes extra interest from another source, making the index too high. This means the borrower is charged less interest than they should be, and their position might not be liquidated when it should be.
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.