The `Vault._update_debt()` function should be executed before admin sets new interest rate via `Vault.set_variable_interest_parameters()`
The Vault contract's set_variable_interest_parameters function updates interest rate configuration without first accruing interest since the last update. This means that when the admin changes the interest rate, the new rate is applied retroactively to the time that has already passed, rather than only to future time, which is unfair to users.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/103
Found by
0xbepresent
Summary
The Vault._update_debt() should be executed before the interests rate are modified by the Vault.set_variable_interest_parameters() function.
Vulnerability Detail
The Vault._update_debt() function helps to accrue interest since the last update. The execution path:
- It calls the function Vault._debt_interest_since_last_update(_debt_token) in order to calculate the token debt interest.
- The
Vault._debt_interest_since_last_update()function uses the Vault._current_interest_per_second() function in order to get the interest per second. - The Vault._current_interest_per_second() calls Vault._interest_rate_by_utilization() in order to get the
interest rate. - The Vault._interest_rate_by_utilization() function calls the Vault._dynamic_interest_rate_low_utilization() or Vault._dynamic_interest_rate_high_utilization depending on the
switch utilization. - The Vault._dynamic_interest_rate_low_utilization() is executed and it calls the Vault._min_interest_rate().
- Then, the Vault._min_interest_rate() function gets the value from the self.interest_configuration[_address][0]
So in the step 6, it gets the interest from the interest_configuration, then the interests are used in order to calculate the token debt. The Vault._update_debt() accrue interest since the last update, the function should be executed before the admin change the interest configuration via Vault.set_variable_interest_parameters() function. The reason is that the time that has already passed must be taken with the previous interest before the admin changes to the new interests rate.
Impact
The protocol should accrue interest since the last update before change the interest rate configuration. If the admin change the interest rate, the new interest rate should be applied to the future time not to the time that has already passed. The time that has already passed should use the previous interest.
It is unfair for the users because users expects interests changes to be applied after they were changed not to the past time the _update_debt has not been executed.
Code Snippet
- The Vault._update_debt() function.
- The Vault._debt_interest_since_last_update function.
- The Vault._current_interest_per_second() function.
- The Vault._interest_rate_by_utilization() function.
- The Vault._dynamic_interest_rate_low_utilization() function.
- The Vault._dynamic_interest_rate_high_utilization() function.
- The Vault._interest_configuration() function.
- The Vault.set_variable_interest_parameters() function.
Tool used
Manual review
Recommendation
Execute self._update_debt(address) before the interests are modified:
@external
def set_variable_interest_parameters(
_address: address,
_min_interest_rate: uint256,
_mid_interest_rate: uint256,
_max_interest_rate: uint256,
_rate_switch_utilization: uint256,
):
assert msg.sender == self.admin, "unauthorized"
++ self._update_debt(address)
self.interest_configuration[_address] = [
_min_interest_rate,
_mid_interest_rate,
_max_interest_rate,
_rate_switch_utilization,
]
Discussion
Unstoppable-DeFi
https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/4
maarcweiss
Fixed by calling update_debt before setting new interest params in the set_variable_interest_parameters function
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
Users could be charged interest at the new rate for a period that occurred before the rate change, which is unfair and could lead to incorrect debt amounts.
What the attacker needed
Admin calls set_variable_interest_parameters
The fix
The fix was to call _update_debt before updating the interest parameters in set_variable_interest_parameters.
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 contract, interest builds up over time. When an admin changes the interest rate, the contract should first calculate and add the interest owed for the time that has already passed using the old rate. This report says the contract skips that step, so the new rate gets applied to past time as well, which can overcharge or undercharge users.
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.