setReserveFactor fails to update global interest before updating reserve factor
The setReserveFactor function updates the reserve factor without first updating global interest, so the new reserve factor is applied retroactively to the period since the last interest update. This causes the lending exchange rate to change unexpectedly, giving lenders a slightly different rate than they should have received.
Submitted by thank_you
https://github.com/code-423n4/2024-03-revert-lend/blob/main/src/V3Vault.sol?plain=1#L1167-L1195
https://github.com/code-423n4/2024-03-revert-lend/blob/main/src/V3Vault.sol?plain=1#L837-L840
Impact
When the vault owner calls V3Vault.setReserveFactor(), the function updates the reserve factor for the Vault. Unfortunately, if the global interest is not updated before the reserve factor is updated, the updated reserve factor will be retroactively applied in the exchange rate formula starting at the last update. This leads to unexpected lending rate changes causing lenders to receive unexpected more or less favorable lending exchange rate depending on what the updated reserve factor value is.
Proof of Concept
The lending rate formula is calculated _calculateGlobalInterest() and the formula can be defined as:
(uint256 borrowRateX96, uint256 supplyRateX96) = interestRateModel.getRatesPerSecondX96(available, debt);
supplyRateX96 = supplyRateX96.mulDiv(Q32 - reserveFactorX32, Q32);
// always growing or equal
uint256 lastRateUpdate = lastExchangeRateUpdate;
if (lastRateUpdate > 0) {
newDebtExchangeRateX96 = oldDebtExchangeRateX96 + oldDebtExchangeRateX96 * (block.timestamp - lastRateUpdate) * borrowRateX96 / Q96;
newLendExchangeRateX96 = oldLendExchangeRateX96 + oldLendExchangeRateX96 * (block.timestamp - lastRateUpdate) * supplyRateX96 / Q96;
} else {
newDebtExchangeRateX96 = oldDebtExchangeRateX96;
newLendExchangeRateX96 = oldLendExchangeRateX96;
}
In the formula above, the supply rate is modified before being used to calculate the new lending rate as supply rate * reserve factor. This modified supply rate is then used to determine how much of a jump should occur in the lending rate via newLendExchangeRateX96 = oldLendExchangeRateX96 + oldLendExchangeRateX96 * (block.timestamp - lastRateUpdate) * supplyRateX96 / Q96. The larger the modified supply rate, the larger the jump is. The smaller the modified supply rate, the smaller the jump is.
By not updating the lending rate before updating the reserve factor, the updated reserve factor will retroactively be applied to the past artificially influencing the lending rate.
To best visualize this, let’s look at the forge test below which shows two scenarios, one where the interest is updated before the reserve factor is updated and one where it’s not. Then we can compare the different lending rate values and see how by not updating the exchange rates before updating the reserve factor, the lending rate is impacted.
RESULTS FROM FORGE TESTS
with interest rate update occurring after reserve factor update:
- starting lendExchangeRateX96: 79243018781103204090820932736
- after reserve update lendExchangeRateX96: 79240047527736122590199028736
with interest rate update occurring before reserve factor update:
- starting lendExchangeRateX96: 79243018781103204090820932736
- after reserve update lendExchangeRateX96: 79243018781103204090820932736
function testLendingRateReserveFactorBugWithoutInterestUpdate() external {
vault.setLimits(1000000, 1000e18, 1000e18, 1000e18, 1000e18);
// set up basic vault settings
_deposit(10000000, WHALE_ACCOUNT);
_setupBasicLoan(true);
vm.warp(block.timestamp + 7 days);
(,,,,,,uint lendExchangeRateX96) = vault.vaultInfo();
console.log("old lendExchangeRateX96: ", lendExchangeRateX96);
vm.prank(vault.owner());
vault.setReserveFactor(uint32(Q32 / 5)); // 20% reserve factor
// AUDIT: Calling setLimits updates the exchange rate
vault.setLimits(1000000, 1000e18, 1000e18, 1000e18, 1000e18);
(,,,,,,lendExchangeRateX96) = vault.vaultInfo();
console.log("new lendExchangeRateX96: ", lendExchangeRateX96);
}
function testLendingRateReserveFactorBugWithInterestUpdate() external {
vault.setLimits(1000000, 1000e18, 1000e18, 1000e18, 1000e18);
// set up basic vault settings
_deposit(10000000, WHALE_ACCOUNT);
_setupBasicLoan(true);
vm.warp(block.timestamp + 7 days);
(,,,,,,uint lendExchangeRateX96) = vault.vaultInfo();
console.log("old lendExchangeRateX96: ", lendExchangeRateX96);
vm.prank(vault.owner());
// AUDIT: Calling setLimits updates the exchange rate
vault.setLimits(1000000, 1000e18, 1000e18, 1000e18, 1000e18);
vault.setReserveFactor(uint32(Q32 / 5)); // 20% reserve factor
(,,,,,,lendExchangeRateX96) = vault.vaultInfo();
console.log("new lendExchangeRateX96: ", lendExchangeRateX96);
}
As you can see, by not updating the interest rate before updating the reserve factor, the lending rate will be impacted unfairly.
Recommended Mitigation Steps
Add _updateGlobalInterest() to the V3Vault.setReserveFactor() function before the reserve factor is updated. This ensures that the lending rate will not be artificially impacted and the updated reserve factor is not retroactively applied to the past:
function setReserveFactor(uint32 _reserveFactorX32) external onlyOwner {
_updateGlobalInterest();
reserveFactorX32 = _reserveFactorX32;
}
Assessed type
Math
The losses are negligible, but it indeed breaks the math.
Fixed here.
Status: Mitigation confirmed. Full details in reports from b0g0, thank_you and ktg.
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 setReserveFactor function does not call _updateGlobalInterest before changing the reserve factor, so the new factor is used when calculating interest for the period since the last update.
Impact
Lenders could receive a slightly more or less favourable lending exchange rate depending on the new reserve factor value; the judge noted the losses are negligible but the math is broken.
What the attacker needed
Vault owner calls setReserveFactor; interest has accrued since last update
The fix
The setReserveFactor function was fixed by adding a call to _updateGlobalInterest before updating the reserve factor.
How it was found
- Set up a vault with a deposit and a loan, then advance time by 7 days.
- Record the current lending exchange rate.
- Call setReserveFactor with a new value (e.g., 20%).
- Trigger an interest update (e.g., by calling setLimits) and observe the changed lending exchange rate.
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 vault, interest builds up over time and is converted into an exchange rate that determines how much lenders can withdraw. The reserve factor is a cut taken from interest. If you change the reserve factor without first recording the interest earned so far, the new cut is applied to past interest as well, which slightly changes the exchange rate. This report shows that the function should update interest first.
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.