If collateral factor is high enough, flutter ends up being out of bounds
The SellUSSDBuyCollateral function in the USSDRebalancer contract calculates a flutter index that can equal the length of the flutterRatios array when the collateral factor exceeds all ratios. This causes an out-of-bounds access when indexing the collateral ratios array, making the function revert and blocking rebalancing.
Source: https://github.com/sherlock-audit/2023-05-USSD-judging/issues/889
Found by
J4de, neumo
Summary
In USSDRebalancer contract, function SellUSSDBuyCollateral will revert everytime a rebalance calls it, provided the collateral factor is greater than all the elements of the flutterRatios array.
Vulnerability Detail
Function SellUSSDBuyCollateral calculates flutter as the lowest index of the flutterRatios array for which the collateral factor is smaller than the flutter ratio.
uint256 cf = IUSSD(USSD).collateralFactor();
uint256 flutter = 0;
for (flutter = 0; flutter < flutterRatios.length; flutter++) {
if (cf < flutterRatios[flutter]) {
break;
}
}
The problem arises when, if collateral factor is greater than all flutter values, after the loop flutter = flutterRatios.length.
This flutter value is used afterwards here:
...
if (collateralval * 1e18 / ownval < collateral[i].ratios[flutter]) {
portions++;
}
...
And here:
...
if (collateralval * 1e18 / ownval < collateral[i].ratios[flutter]) {
if (collateral[i].token != uniPool.token0() || collateral[i].token != uniPool.token1()) {
// don't touch DAI if it's needed to be bought (it's already bought)
IUSSD(USSD).UniV3SwapInput(collateral[i].pathbuy, daibought/portions);
}
}
...
As we can see in the tests of the project, the flutterRatios array and the collateral ratios array are set to be of the same length, so if flutter = flutterRatios.length, any call to that index in the ratios array will revert with an index out of bounds.
Impact
High, when the collateral factor reaches certain level, a rebalance that calls SellUSSDBuyCollateral will always revert.
Code Snippet
Tool used
Manual review.
Recommendation
When checking collateral[i].ratios[flutter] always check first that flutter is < flutterRatios.length.
Discussion
neumoxx
Escalate for 10 USDC
The issue is marked as duplicate of #940, and in that issue there's a comment from the judge that states This is an admin input, requires admin error to cause problems. The issue does not depend on an admin input to arise. The flutter ratios are set in the tests according to values mentioned in the whitepaper: https://github.com/sherlock-audit/2023-05-USSD/blob/6d7a9fdfb1f1ed838632c25b6e1b01748d0bafda/ussd-contracts/test/USSDsimulator.test.js#L391-L393.
The collateral factor:
https://github.com/sherlock-audit/2023-05-USSD/blob/6d7a9fdfb1f1ed838632c25b6e1b01748d0bafda/ussd-contracts/contracts/USSD.sol#L179-L191
can grow beyond the last value of the flutter ratios array and that would make the SellUSSDBuyCollateral function to revert.
sherlock-admin
Escalate for 10 USDC The issue is marked as duplicate of #940, and in that issue there's a comment from the judge that states
This is an admin input, requires admin error to cause problems. The issue does not depend on an admin input to arise. The flutter ratios are set in the tests according to values mentioned in the whitepaper: https://github.com/sherlock-audit/2023-05-USSD/blob/6d7a9fdfb1f1ed838632c25b6e1b01748d0bafda/ussd-contracts/test/USSDsimulator.test.js#L391-L393. The collateral factor: https://github.com/sherlock-audit/2023-05-USSD/blob/6d7a9fdfb1f1ed838632c25b6e1b01748d0bafda/ussd-contracts/contracts/USSD.sol#L179-L191 can grow beyond the last value of the flutter ratios array and that would make theSellUSSDBuyCollateralfunction to revert.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
ctf-sec
function setFlutterRatios(uint256[] calldata _flutterRatios) public onlyControl {
flutterRatios = _flutterRatios;
}
flutterRatios can be adjusted by admin
Valid low
hrishibhat
Result: Medium Has duplicates This is a valid issue where the rebalance reverts in certain conditions due to the unexpected final loop flutter values.
sherlock-admin
Escalations have been resolved successfully!
Escalation status:
- neumoxx: accepted
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
When the collateral factor exceeds all flutter ratios, any rebalance that calls SellUSSDBuyCollateral will revert, potentially disrupting the protocol's stability mechanism.
What the attacker needed
Collateral factor exceeds all flutter ratios
The fix
The code should check that flutter is less than flutterRatios.length before using it to index the ratios array.
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.
Smart contracts often use arrays to store values. If code tries to access an array at an index equal to its length, it causes an error and the transaction fails. Here, a calculation could produce such an invalid index, so a function that rebalances the system would always fail under certain conditions.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.