Wrong type casting leads to unsigned integer underflow exception when current price is < last price
The Vault contract subtracts two unsigned integers before casting the result to a signed integer, so when the current price is lower than the last stored price the subtraction underflows and the transaction reverts. This causes the rebalance function to fail whenever a token's price has fallen.
Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/334
Found by
gogo
Summary
When the current price of a locked token is lower than the last price, the Vault.storePriceAndRewards will revert because of the wrong integer casting.
Vulnerability Detail
The following line appears in Vault.storePriceAndRewards:
int256 priceDiff = int256(currentPrice - lastPrices[_protocolId]);
If lastPrices[_protocolId] is higher than the currentPrice, the solidity compiler will revert due the underflow of subtracting unsigned integers because it will first try to calculate the result of currentPrice - lastPrices[_protocolId] and then try to cast it to int256.
Impact
The rebalance will fail when the current token price is less than the last one stored.
Code Snippet
int256 priceDiff = int256(currentPrice - lastPrices[_protocolId]);
Tool used
Manual Review
Recommendation
Casting should be performed in the following way to avoid underflow and to allow the priceDiff being negative:
int256 priceDiff = int256(currentPrice) - int256(lastPrices[_protocolId]));
Discussion
gogotheauditor
Escalate for 10 USDC
Although I didn't have much time for this contest and may be missing some knowledge about the whole flow, I believe this finding is incorrectly downgraded from High to Medium. As mentioned in the docs for the rebalance() function (which calls rebalanceCheckProtocols() which calls the problematic storePriceAndRewards() for each protocol), "This is the most important function of the vault." This finding shows that the vault rebalancing will not work when the current price of any of the protocols' LP tokens reaches a high price and the new price has not increased since then. During a bear market, this could mean that the vault rebalancing won't work for a long time, while the docs say it should be "triggered once per two weeks." Please review and provide feedback, @sjoerdsommen @judge.
sherlock-admin
Escalate for 10 USDC Although I didn't have much time for this contest and may be missing some knowledge about the whole flow, I believe this finding is incorrectly downgraded from High to Medium. As mentioned in the docs for the
rebalance()function (which callsrebalanceCheckProtocols()which calls the problematicstorePriceAndRewards()for each protocol), "This is the most important function of the vault." This finding shows that the vault rebalancing will not work when the current price of any of the protocols' LP tokens reaches a high price and the new price has not increased since then. During a bear market, this could mean that the vault rebalancing won't work for a long time, while the docs say it should be "triggered once per two weeks." Please review and provide feedback, @sjoerdsommen @judge.
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.
Theezr
Fix: https://github.com/derbyfinance/derby-yield-optimiser/pull/195
hrishibhat
Escalation accepted
Considering this issue a valid high because rebalance is important and currentprice can be less than the last price regularly resulting in not being able to rebalance.
sherlock-admin
Escalation accepted
Considering this issue a valid high because rebalance is important and currentprice can be less than the last price regularly resulting in not being able to rebalance.
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
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 cast to int256 was applied after the unsigned subtraction, so the subtraction underflowed before the value could become negative.
Impact
Rebalancing fails whenever the current token price is less than the last stored price, which the report says can happen regularly, for example in a bear market.
What the attacker needed
Current price below last stored price
The fix
The fix casts each price to int256 before subtracting, so the difference can be negative; a pull request addressing it is linked in the discussion.
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.
Solidity numbers come in two kinds: unsigned, which cannot go below zero, and signed, which can be negative. The code subtracted two unsigned prices first and only then tried to convert the answer to a signed number, so a falling price made the subtraction wrap around and the whole transaction failed. That meant the vault could not rebalance its investments during a price drop.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.