Attackers can force surge to never update the collateralization ratio
The collateralization ratio update calculation can be forced to zero by frequent small deposits, preventing the ratio from ever changing. An attacker could keep the pool's price stale, potentially causing losses for depositors.
Source: https://github.com/sherlock-audit/2023-02-surge-judging/issues/109
Found by
usmannk
Summary
Certain parameter choices make it feasible to block updates to the collateralization ratio. Collaterization ratio updates are calculated as uint change = timeDelta * _maxCollateralRatioMantissa / _collateralRatioRecoveryDuration; . However, with quick refreshes or a _collateralRatioRecoveryDuration that is greater than _maxCollateralRatioMantissa, this change may be zero every iteration.
Vulnerability Detail
https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L216-L263
The getCollateralRatioMantissa function calculates the collateralization ratio by linearly updating along _maxCollateralRatioMantissa / _collateralRatioRecoveryDuration. However, these updates may be forced to zero in certain situations.
Consider a pool where the loan token is WBTC and the collateral token is DAI. Given a BTC price of $20,000 it is reasonable to only allow 1/10000 BTC to be borrowed per DAI (for a max rate of $10,000 per BTC).
The _maxCollateralRatioMantissa in this case would be 1e14. In the Surge tests, a _collateralRatioRecoveryDuration of 1e15 is used. If an attacker does a tiny deposit of 1wei WBTC more often than once every 10 seconds, the change of the max collateralization ratio will always be zero no matter what the current utilization is because (timeDelta * _maxCollateralRatioMantissa) is less than _collateralRatioRecoveryDuration.
This would halt the entire adaptive pricing scheme of the Surge protocol while still allowing borrows at the current rate.
The README specifies that Surge is meant to be deployed on DEPLOYMENT: Mainnet, Optimism, Arbitrum, Fantom, Avalanche, Polygon, BNB Chain and other EVM chains. This exploit is especially attractive on L2s because of cheap/free execution (e.g. Optimism) and very low block times (thus low timeDelta).
Impact
Loss of funds for depositors as the price becomes stale and the collateralization rate, and thus pool exchange rate, of the Surge pool would no longer update.
Code Snippet
Tool used
Manual Review
Recommendation
Ensure that _collateralRatioRecoveryDuration < _maxCollateralRatioMantissa. This would preclude some pools from existing, but save funds from being stolen.
Discussion
hrishibhat
Given the unlikely edge case of having a pool with an edge case mentioned by the Sponsor:
a legit pool might have recovery duration set to max uint in case lenders wouldn't want the collateral factor to ever rise back up after falling
Considering this issue as a valid medium
xeious
Fixed
xeious
https://github.com/Surge-fi/surge-protocol-v1/commit/fc0fb9fa203ecf7c9b8415b9d451dba56d5ef832
IAm0x52
Fix looks good. Requires _collateralRatioFallDuration and _collateralRatioRecoveryDuration be less than _maxCollateralRatioMantissa.
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 integer division truncation: the change per update is computed as (timeDelta * maxCollateralRatioMantissa) / collateralRatioRecoveryDuration, and when timeDelta is small or the denominator is large, the result is zero.
Impact
The pool's collateralization ratio and exchange rate become stale, potentially causing loss of funds for depositors; the exploit is especially attractive on L2s with cheap execution and low block times.
What the attacker needed
Pool with specific parameter choices; attacker can make frequent tiny deposits; low transaction cost (e.g., L2)
The fix
The fix requires that _collateralRatioFallDuration and _collateralRatioRecoveryDuration be less than _maxCollateralRatioMantissa.
How it was found
- Choose a pool where the loan token is WBTC and collateral token is DAI, with a max collateral ratio mantissa of 1e14 and recovery duration of 1e15.
- Perform a tiny deposit of 1 wei WBTC more frequently than once every 10 seconds.
- Observe that the collateralization ratio change remains zero, halting adaptive pricing.
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 pool's collateral ratio adjusts over time to reflect risk. The code calculates the adjustment by multiplying elapsed time by a rate, but if the time between updates is very short, the result rounds down to zero. An attacker can make tiny deposits often to keep the time between updates short, so the ratio never moves.
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.