Vault.rebalance calculates rewards per locked token incorrectly
The Vault.rebalance function updates the total allocated tokens before calculating rewards, so rewards are computed using the new period's allocation instead of the previous period's. This causes rewards for all reward cycles to be incorrect.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/105
Found by
rvierdiiev
Summary
Vault.rebalance calculates rewards per locked token incorrectly, because it calls settleDeltaAllocation function, which updates totalAllocatedTokens variable with new allocations, before storePriceAndRewards function, which calculates rewards based on totalAllocatedTokens amount.
Vulnerability Detail
Function storePriceAndRewards is responsible for calculating rewards per locked token for provider.
To calculate rewards it uses totalAllocatedTokens variable. This variable should be allocated amount for previous period.
Function storePriceAndRewards is called inside rebalanceCheckProtocols function, which is called inside rebalance function.
The problem is that before rebalanceCheckProtocols function, the settleDeltaAllocation function is called and this function is actually updates totalAllocatedTokens with new allocations.
This is the problem as storePriceAndRewards function should calculate rewards based on totalAllocatedTokens amount for previous period, but because settleDeltaAllocation is called before, totalAllocatedTokens amount corresponds to the new period and calculations are incorrect.
Impact
Rewards for all reward cycles are incorrect.
Code Snippet
Provided above
Tool used
Manual Review
Recommendation
You need to calculate rewards before changing totalAllocatedTokens variable.
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 function `settleDeltaAllocation` updates `totalAllocatedTokens` before `storePriceAndRewards` uses it to calculate rewards, so the rewards are based on the new allocation rather than the previous period's allocation.
Impact
Rewards for all reward cycles are incorrect.
What the attacker needed
None
The fix
Calculate rewards before changing the `totalAllocatedTokens` variable.
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 smart contract, the order in which functions are called matters. Here, the contract updates a total amount before using that total to calculate rewards, so the rewards are based on the wrong number. This means everyone's rewards are calculated incorrectly.
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.