Debt position interest is compounded while pool interest is simple causing inconsistency between expectedLiquidity_ and availableLiquidity_
The debt position interest is compounded while the pool interest is simple, causing borrowers to pay more interest than the pool expects. This inconsistency can lead to lowered borrowing rates and final withdrawals reverting due to underflow in expectedLiquidity.
hash Follow with Afriauditor and monrel
Originally disclosed on Code4rena
Submitted by hash, also found by Afriauditor and monrel
Borrower’s will mostly end up paying more than the required amount of interest. This can also lead to lowered borrowing interest rates and final withdrawals to revert due to the inconsistency between the expected interest amount and the actually paid interest amount.
Proof of Concept
The interest associated with a debt is calculated both in the pool and the user’s debt position. However, the used method for calculation leads to different values between the pool and the debt position. In the pool it is simple linear interest, while for the debt position, the interest gets compounded.
In the pool the interest is calculated as borrowed * interestRate * (elapsedTime/365 days):
function _calcBaseInterestAccrued(uint256 timestamp) private view returns (uint256) {
return (_totalDebt.borrowed * baseInterestRate().calcLinearGrowth(timestamp)) / RAY;
}
In the CDP vault (i.e., user’s debt position), the interest is calculated as debt * interestIndexNow / interestIndexPast - debt:
function _calcDebt(Position memory position) internal view returns (DebtData memory cdd) {
....
cdd.accruedInterest = CreditLogic.calcAccruedInterest(cdd.debt, cdd.cumulativeIndexLastUpdate, index);
function calcAccruedInterest(uint256 amount, uint256 cumulativeIndexLastUpdate, uint256 cumulativeIndexNow)
internal
pure
returns (uint256)
{
if (amount == 0) return 0;
return (amount * cumulativeIndexNow) / cumulativeIndexLastUpdate - amount; // U:[CL-1]
}
Where the interestIndex is updated as follows:
function _calcBaseInterestIndex(uint256 timestamp) private view returns (uint256) {
return (_baseInterestIndexLU * (RAY + baseInterestRate().calcLinearGrowth(timestamp))) / RAY;
}
Hence, the interestIndex gets compounded with every invocation of _calcBaseInterestIndex, eventually causing a higher interest for the debt position when compared with the pool calculation. This also causes incorrectness between expectedLiquidity_ and availableLiquidity_ (i.e., expectedLiquidity_ will be less than availableLiquidity_).
The correct relation between expectedLiquidity_ and availableLiquidity_ is required for _updateBaseInterest which is used throughout the contract for purposes like withdrawals:
function _withdraw(
address receiver,
address owner,
uint256 assetsSent,
uint256 assetsReceived,
uint256 amountToUser,
uint256 shares
) internal {
if (msg.sender != owner) _spendAllowance({owner: owner, spender: msg.sender, amount: shares}); // U:[LP-8,9]
_burn(owner, shares); // U:[LP-8,9]
_updateBaseInterest({
expectedLiquidityDelta: -assetsSent.toInt256(),
availableLiquidityDelta: -assetsSent.toInt256(),
checkOptimalBorrowing: false
}); // U:[LP-8,9]
If the associated shares with the excess interest are withdrawn, it will cause an underflow for expectedLiquidity during final withdrawals
POC Code
Apply the following diff and run forge test --mt testHash_CompoundingVsSimpleInterest. It is asserted that the calculated interest in the position is more than in the pool and that final withdrawals can revert due to this. The minRate == 0 check in GaugeV3 contract is commented out in order to keep quotaInterest to 0 so that the only interest being accrued is the base interest, which will make it easier for displaying the difference in its calculation.
diff --git a/src/quotas/GaugeV3.sol b/src/quotas/GaugeV3.sol
index e16d6dc..00574a9 100644
--- a/src/quotas/GaugeV3.sol
+++ b/src/quotas/GaugeV3.sol
@@ -303,9 +303,9 @@ contract GaugeV3 is IGaugeV3, ACLNonReentrantTrait {
/// @dev Checks that given min and max rate are correct (`0 < minRate <= maxRate`)
function _checkParams(uint16 minRate, uint16 maxRate) internal pure {
- if (minRate == 0 || minRate > maxRate) {
- revert IncorrectParameterException(); // U:[GA-04]
- }
+ // if (minRate == 0 || minRate > maxRate) {
+ // revert IncorrectParameterException(); // U:[GA-04]
+ // }
}
/// @notice Whether token is added to the gauge as quoted
diff --git a/src/test/unit/CDPVault.t.sol b/src/test/unit/CDPVault.t.sol
index 85c82ca..6e324a1 100644
--- a/src/test/unit/CDPVault.t.sol
+++ b/src/test/unit/CDPVault.t.sol
@@ -166,6 +166,81 @@ contract CDPVaultTest is TestBase {
assertEq(credit, 50 ether);
}
+ function HashCreateGaugeAndSetGauge(address vault) internal virtual {
+ address token_ = address(CDPVault(vault).token());
+ HashCreateGaugeAndSetGauge(vault, token_);
+ }
+
+ function HashCreateGaugeAndSetGauge(address vault, address token_) internal virtual {
+ quotaKeeper.setCreditManager(address(token_), address(vault));
+ if (!gauge.isTokenAdded(address(token_))) {
+ gauge.addQuotaToken(address(token_), 0, 1);
+ }
+ gauge.setFrozenEpoch(false);
+ vm.warp(block.timestamp + 1 weeks);
+ vm.prank(address(gauge));
+ quotaKeeper.updateRates();
+ }
+
+ function testHash_CompoundingVsSimpleInterest() public {
+ CDPVault vault = createCDPVault(token, 100 ether, 10 ether, 1.25 ether, 1.0 ether, 0);
+ // the qouta interest is set to 0
+ HashCreateGaugeAndSetGauge(address(vault));
+ token.mint(address(this), 100 ether);
+ token.approve(address(vault), 100 ether);
+ address position = address(new PositionOwner(vault));
+ vault.deposit(position, 100 ether);
+
+ uint quotaRevenue = liquidityPool.quotaRevenue();
+ assert(quotaRevenue == 0);
+
+ uint initialExpectedAmount = liquidityPool.expectedLiquidity();
+
+ vault.borrow(address(this), position, 50 ether);
+
+ // first interest index update
+ vm.warp(block.timestamp + 1 days);
+ liquidityPool.deposit(0,address(this));
+ // second interest index update
+ vm.warp(block.timestamp + 1 days);
+ liquidityPool.deposit(0,address(this));
+
+ // quota revenue is 0
+ quotaRevenue = liquidityPool.quotaRevenue();
+ assert(quotaRevenue == 0);
+
+ uint newExpectedAmount = liquidityPool.expectedLiquidity();
+ uint poolAccruedInterest = newExpectedAmount - initialExpectedAmount;
+
+ (uint256 debt, uint256 positionAccruedInterest, uint256 cumulativeQuotaInterest)=vault.getDebtInfo(position);
+
+ assert(debt == 50 ether);
+ assert(cumulativeQuotaInterest == 0);
+ assert(positionAccruedInterest > poolAccruedInterest);
+
+ // this additional interest will is not accounted in the expected interest
+
+ mockWETH.mint(address(this), debt + positionAccruedInterest);
+ mockWETH.approve(address(vault), debt + positionAccruedInterest);
+ vault.repay(address(this),position,debt + positionAccruedInterest);
+
+ // attempt to withdraw all the tokens from the pool will revert since the expectedAmount will underflow
+ {
+ liquidityPool.setLock(false);
+ uint balTreasury = liquidityPool.balanceOf(treasury);
+ uint balThis = liquidityPool.balanceOf(address(this));
+ assert(balTreasury + balThis == liquidityPool.totalSupply());
+
+ vm.prank(treasury);
+ liquidityPool.withdraw(balTreasury,treasury,treasury);
+
+ vm.expectRevert("SafeCast: value must be positive");
+ liquidityPool.withdraw(balThis,address(this),address(this));
+ }
+
+
+ }
+
function test_modifyCollateralAndDebt_depositCollateralAndDrawDebt() public {
CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1.0 ether, 0);
createGaugeAndSetGauge(address(vault));
Recommended Mitigation Steps
Change the index updation to align with the pool calculation (i.e., interestIndex = prevInterestIndex + baseInterest * timeElapsed.
Assessed type
Math
Koolex (judge) increased severity to High
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 interest index for debt positions is updated multiplicatively (compounding), while the pool calculates interest linearly, leading to divergent interest amounts.
Impact
Borrowers pay more interest than the pool accounts for, which can lower borrowing rates and cause final withdrawals to revert due to underflow in expectedLiquidity.
What the attacker needed
A CDP vault with borrowing enabled; a gauge with zero quota interest; time passes to accrue interest
The fix
The interest index should be updated linearly to match the pool's simple interest calculation.
How it was found
- Create a CDP vault and set up a gauge with zero quota interest.
- Deposit collateral and borrow tokens from the vault.
- Advance time and trigger interest index updates by making zero-amount deposits to the pool.
- Observe that the position's accrued interest exceeds the pool's accrued interest.
- Repay the debt plus the position's accrued interest.
- Attempt to withdraw all pool tokens; the final withdrawal reverts due to underflow in expectedLiquidity.
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.
Interest on a loan can be calculated in two ways: simple interest adds a fixed percentage of the original amount each period, while compound interest also charges interest on previously accrued interest. Here, the pool used simple interest but the borrower's debt used compound interest, so the borrower owed more than the pool had recorded. This mismatch could cause problems when people try to withdraw all their funds.
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.