`ZivoeTranches#rewardZVEJuniorDeposit` function miscalculates the reward when the ratio traverses lower/upper bound
The reward calculation in ZivoeTranches uses the average of the start and final ratios, which can cause the reward to be miscalculated when the ratio crosses the lower or upper incentive bound. An attacker could exploit this by splitting deposits to receive more rewards than intended, leading to a loss of funds for the protocol or the depositor.
AMOW Follow with amar and dany.armstrong90
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/232
The protocol has acknowledged this issue.
Found by
AMOW, amar, dany.armstrong90
Summary
ZivoeTranches#rewardZVEJuniorDeposit function miscalculates the reward when the ratio traverses lower/upper bound.
The same issue also exists in the ZivoeTranches#rewardZVESeniorDeposit function.
Vulnerability Detail
ZivoeTranches#rewardZVEJuniorDeposit function is the following.
function rewardZVEJuniorDeposit(uint256 deposit) public view returns (uint256 reward) {
(uint256 seniorSupp, uint256 juniorSupp) = IZivoeGlobals_ZivoeTranches(GBL).adjustedSupplies();
uint256 avgRate; // The avg ZVE per stablecoin deposit reward, used for reward calculation.
uint256 diffRate = maxZVEPerJTTMint - minZVEPerJTTMint;
uint256 startRatio = juniorSupp * BIPS / seniorSupp;
uint256 finalRatio = (juniorSupp + deposit) * BIPS / seniorSupp;
213: uint256 avgRatio = (startRatio + finalRatio) / 2;
if (avgRatio <= lowerRatioIncentiveBIPS) {
216: avgRate = maxZVEPerJTTMint;
} else if (avgRatio >= upperRatioIncentiveBIPS) {
218: avgRate = minZVEPerJTTMint;
} else {
220: avgRate = maxZVEPerJTTMint - diffRate * (avgRatio - lowerRatioIncentiveBIPS) / (upperRatioIncentiveBIPS - lowerRatioIncentiveBIPS);
}
223: reward = avgRate * deposit / 1 ether;
// Reduce if ZVE balance < reward.
if (IERC20(IZivoeGlobals_ZivoeTranches(GBL).ZVE()).balanceOf(address(this)) < reward) {
reward = IERC20(IZivoeGlobals_ZivoeTranches(GBL).ZVE()).balanceOf(address(this));
}
}
Here, let us assume that lowerRatioIncentiveBIPS = 1000, upperRatioIncentiveBIPS = 2500, minZVEPerJTTMint = 0, maxZVEPerJTTMint = 0.4 * 10 ** 18, seniorSupp = 10000.
Let us consider the case of juniorSupp = 0 where the ratio traverses the lower bound.
Example 1:
Assume that the depositor deposit 2000 at a time.
Then avgRatio = 1000 holds in L213, thus avgRate = maxZVEPerJTTMint = 0.4 * 10 ** 18 holds in L216.
Therefore reward = 0.4 * deposit = 800 holds in L223.
Example 2:
Assume that the depositor deposit 1000 twice.
Then, since avgRate = 500 < lowerRatioIncentiveBIPS holds for the first deposit, avgRate = 0.4 * 10 ** 18 holds in L216, thus reward = 400 holds.
Since avgRate = 1500 > lowerRatioIncentiveBIPS holds for the second deposit, avgRate = 0.3 * 10 ** 18 holds in L220, thus reward = 300 holds.
Finally, the total sum of rewards for two deposits are 400 + 300 = 700.
This shows that the reward of the case where all assets are deposited at a time is larger than the reward of the case where assets are divided and deposited twice. In this case, the protocol gets loss of funds.
Likewise, in the case where the ratio traverses the upper bound, the reward of one time deposit will be smaller than the reward of two times deposit and thus the depositor gets loss.
The same issue also exists in the ZivoeTranches#rewardZVESeniorDeposit function.
Impact
When the ratio traverses the lower/upper bound in ZivoeTranches#rewardZVEJuniorDeposit and ZivoeTranches#rewardZVESeniorDeposit functions, the amount of reward will be larger/smaller than it should be. Thus the depositor or the protocol will get loss of funds.
Code Snippet
Tool used
Manual Review
Recommendation
Modify the functions to calculate the reward dividing into two portion when the ratio traverses the lower/upper bound, which is similar to the case of Example 2.
Discussion
pseudonaut
Yes if the tranche ratio's swing very far out it will allow for calculations of this nature - known, not of concern
sherlock-admin3
1 comment(s) were left on this issue during the judging contest.
panprog commented:
borderline low/medium, while true, the impact is not very large and can be considered protocol's intended choice to simplify the curve. From protocol's standpoint this is simply incentive to deposit and slightly more or less in specific circumstances is not that important.
panprog
Downgrading to Low.
This is known to developers and I consider this a design decision to simplify the calculations / save gas. The user can be informed about such behaviour and can do multiple or single transaction depending on what's more beneficial. No funds loss from protocol's standpoint (simply slightly different ratio of rewards distribution which doesn't influence anything)
armormadeofwoe
Continuing discussion here.
Here is a graph of the juniorReward linear decrease with respect to the min/max reward thresholds. As mentioned in this issue and its' 2 dups - a 2-deposit split where the initial one is crafted to not cross the max threshold will always yield more rewards than going at once. I believe this issue's math paired with a visualization should be sufficient. Let me know your thoughts.
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 the use of the average of start and final ratios to determine the reward rate, which does not account for the non-linear reward curve when the ratio crosses the lower or upper bound.
Impact
When the ratio traverses the lower or upper bound, the reward amount will be larger or smaller than it should be, causing a loss of funds for either the depositor or the protocol; however, the protocol has acknowledged this as a known design choice and considers it low severity.
What the attacker needed
JuniorSupp = 0; deposit amount causes ratio to cross bound
The fix
The functions should be modified to calculate the reward by dividing into two portions when the ratio traverses the lower or upper bound, similar to the case of Example 2.
How it was found
- Assume lowerRatioIncentiveBIPS = 1000, upperRatioIncentiveBIPS = 2500, minZVEPerJTTMint = 0, maxZVEPerJTTMint = 0.4 * 10 ** 18, seniorSupp = 10000.
- Set juniorSupp = 0.
- Deposit 2000 at once: avgRatio = 1000, avgRate = maxZVEPerJTTMint, reward = 800.
- Deposit 1000 twice: first deposit avgRate = 500 < lowerRatioIncentiveBIPS, avgRate = maxZVEPerJTTMint, reward = 400; second deposit avgRate = 1500 > lowerRatioIncentiveBIPS, avgRate = 0.3 * 10 ** 18, reward = 300; total reward = 700.
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.
This report is about a smart contract that gives rewards for depositing tokens. The reward rate depends on the ratio of two pools. The code calculates the reward using an average ratio, which can be wrong if the ratio crosses certain thresholds during the deposit. By splitting a deposit into smaller ones, a user can get a different total reward than if they deposited all at once, which can be unfair to either the user or the protocol.
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.