`ZivoeYDL::earningsTrancheuse()` always assumes that `daysBetweenDistributions` have passed, which might not be the case
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/347
The protocol has acknowledged this issue.
Found by
0x73696d616f
Summary
ZivoeYDL::earningsTrancheuse() calculates the target yield, senior and junior proportions based on daysBetweenDistributions. However, it is not strictly enforced that ZivoeYDL::distributeYield() is called exactly at the daysBetweenDistributions mark, leading to less yield for tranches.
Vulnerability Detail
There is a yield target for senior and junior tranches over a daysBetweenDistributions period that when exceeded, sends the rewards to residuals. However, the current code will not strictly follow the expected APY for senior tranches, as the earning of tranches are calculated based on daysBetweenDistributions, but ZivoeYDL::distributeYield() may be called significantly after the daysBetweenDistributions mark.
Add the following test to Test_ZivoeYDL.sol to confirm that when the senior tranche yield reached the expected target for 30 days, if the time that ZivoeYDL::distributeYield() was called increased, the yield distributed to the senior tranch remains the same.
function test_POC_ZivoeYDL_distributeYield_notEnforcingTranchesPeriod() public {
uint256 amtSenior = 1500 ether; // Minimum amount $1,000 USD for each coin.
uint256 amount = 1000 ether;
// Simulating the ITO will "unlock" the YDL
simulateITO_byTranche_optionalStake(amtSenior, true);
// uncomment this line instead to confirm that it's the same value below
//hevm.warp(YDL.lastDistribution() + YDL.daysBetweenDistributions() * 86400);
hevm.warp(YDL.lastDistribution() + YDL.daysBetweenDistributions() * 86400 + 1 days);
// Deal DAI to the YDL
mint("DAI", address(YDL), amount);
YDL.distributeYield();
console.log(IERC20(DAI).balanceOf(address(stSTT)));
}
Impact
Guaranteed less APY for junior and senior tranches. The amount depends on how much time the call to ZivoeYDL::distributeYield() was delayed since block.timestamp reached lastDistribution + daysBetweenDistributions * 86400.
Code Snippet
function distributeYield() external nonReentrant {
...
require(
block.timestamp >= lastDistribution + daysBetweenDistributions * 86400,
"ZivoeYDL::distributeYield() block.timestamp < lastDistribution + daysBetweenDistributions * 86400"
);
...
ZivoeYDL::earningsTrancheuse()
function earningsTrancheuse(uint256 yP, uint256 yD) public view returns (
uint256[] memory protocol, uint256 senior, uint256 junior, uint256[] memory residual
) {
...
uint256 _seniorProportion = MATH.seniorProportion(
IZivoeGlobals_YDL(GBL).standardize(yD, distributedAsset),
MATH.yieldTarget(emaSTT, emaJTT, targetAPYBIPS, targetRatioBIPS, daysBetweenDistributions),
emaSTT, emaJTT, targetAPYBIPS, targetRatioBIPS, daysBetweenDistributions
);
...
}
Tool used
Manual Review
Vscode
Foundry
Recommendation
Modify the ZivoeMath::seniorProportion() and ZivoeMath::yieldTarget() implementations to use seconds instead of days for T. Then, in Zivoe::YDL::earningsTrancheuse(), instead of daysBetweenDistributions when calculating the yield target and senior proportion, use block.timestamp - lastDistribution. Also, in ZivoeYDL::distributeYield(), lastDistribution = block.timestamp; must be moved after earningsTrancheuse() is called.
Discussion
pseudonaut
It will called immediately when possible, so there may be few blocks of lag, acceptable
sherlock-admin4
1 comment(s) were left on this issue during the judging contest.
panprog commented:
invalid, trusted keepers will call it as soon as possible, and even there is a slight delay due to network congestion or whatever else, this is acceptable and intended
0x73696d616f
Escalate
This should be valid as in the first distribution period, the duration is 60 instead of 30, calculating a much smaller APR than it should be. Half the APR is calculated instead of the intended, a substancial amount, which means this should be a valid medium according to the docs.
sherlock-admin3
Escalate
This should be valid as in the first distribution period, the duration is 60 instead of 30, calculating a much smaller APR than it should be. Half the APR is calculated instead of the intended, a substancial amount, which means this should be a valid medium according to the docs.
You've created a valid escalation!
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.
panprog
This is still invalid/low. The 30-days yield for the first distribution which is after 60 days is also intended according to sponsor, see for example #531 which is about the same.
0x73696d616f
Where did the sponsor state or are there any docs showing that the APR should also be half? The sponsor only mentioned the duration as intended.
panprog
This was in a private discussion to confirm the intended behavior.
0x73696d616f
Can't argue with that.
In any case, just a small delay of 5 minutes, will lead to more than 1 BIPS of loss of rewards, as the duration of 30 days is relatively small.
(5 / (30*24*60))*10000 = 1.15. The delay may be bigger in certain sporadic events, leading to even bigger losses.
0x73696d616f
Additionally, the sponsor may say it's acceptable but it does not mean it is not a significant amount nor invalidates the issue.
0x73696d616f
This article, for example, mentions an outage for 1 hour, which is (1 / (30*24))*10000 = 14 BPS. Much more significant than dust or precision, as the docs mention to be a valid medium issue.
WangSecurity
Firstly, I agree that there is no guarantee the keeper will make the call instantly, and there might be delay. But, for that I need to understand what are the losses. I see in this comment you say that in 5 mins it will lose 1 BIPS in terms of rewards, which is 0.01% of the rewards lost, correct? And if it's less than 5 mins, then it'll be less than 1 BIPS. But, the longer the delay, the larger the loss, correct?
0x73696d616f
@WangSecurity exactly.
panprog
@WangSecurity Example:
- Tranche has $3600 deposited. Target APR = 10%, meaning it's $30 per month.
- The earnings for previous period = $35. $30 is deposited to tranche ZivoeReward and distributes at a rate of $1/day (the rest of it - $5 - goes to residual recepients)
- The earnings for the next period = $32. However, the keeper failed to call it in time and called only after 1 day. Since the distribution was $1/day for 30 days, it stops after 30 days and it's $0 for the following day.
- The keeper finally calls
distributeYieldafter 1 day of delay, but it distributes $30 again (not $31 to account for extra day), $2 goes to residuals.
This means that due to 1 day of keeper delay, tranche stakers will receive $30 over 31 days instead of $31. This is 1/31 = 3.2% loss. However, perhaps a more correct way to calculate is to compare the target APR with actual APR. So target = 10%, actual = 30/31*356/3600 = 9.57%, a loss of 0.43% APR per day of delay or a loss of 0.0003% APR per minute of delay.
Since such keeper delay is not very likely, it's better to estimate a total time of keeper delay over the year. In such case the APR loss is 1/3600 = 0.028% per day of delay. Again, this is only if distributed yield exceeds the target APR.
One more thing to consider is that the target rates can be changed by admin via updateTargetAPYBIPS and updateTargetRatioBIPS, so admin can compensate for these delays by increasing target rate if necessary.
CergyK
Additional points to take into consideration when evaluating this issue:
distributeYieldis an unpermissioned action, so if the impact of keeper downtime is deemed unacceptable by any actor, they can ensure it is called timely themselves.- A mainnet outage event of which 2 happened last year, would have to coincide exactly with the beginning of a
distributeYieldunlocking, which is quite unlikely (1 hour / 30 days = 0.01% chance of overlap)
0x73696d616f
@panprog your comment is highly misleading. Firstly, you make a logic error (I hope it's a mistake), which will be explained in detail. Then, replace the 3600 by 360_000, and now the loss is 100 USD. Also, the previous and next periods are not required for this discussion, only the current distribution. There is no need to add extra complexity. Let's break it down.
The expected monthly APR is pro-rata to the time passed, which means the error % will be the same. So a 3.2% error in the number of days will lead to a 3.2% APR error during the 31 days. Check the math at the end of the comment for confirmation. This is the first mistake.
Now, the second mistake, which is much more serious, is that @panprog is calculating the error in ABSOLUTE terms, not relative. This makes no sense. For example, if the target APR was 0.1%, the actual APR would be 0.001 - 0.001*0.032 = 0.000968, just a difference of 0.0032%. This is obviously wrong and extremely misleading. The actual error is calculated in relative terms, similar to what is done when calculating the days error as 1/31, and not just 1.
So, if the target APR is 10%, but the actual is 9.68%, the error in % is (target - actual) / target = (0.1 - 0.0968) / 0.1 = 0.032, or 3.2%.
Additionally, it makes no sense to compare to the yearly APR because e would have to sum all the delays in the year to calculate it, not just use the delay of a single month.
Math proof that the APR error is always the same as the number of days error:
Target USD for 31 days, USDt, is USDt = 31 / 365 * 3600 * 0.1. 31 is the number of days, 365 the number of days in a year, used in the calculation and 0.1 is 10%.
Now, the actual % in these 31 days, where USDa will be the actual USD in the 31 days:
USDa = 3600 * 0.1 * 30 / 365. This is because during the 31 days, only 30 is distributed.
To get the corresponding %, we invert the formula above, where 0.1 is the target % (10%)
0.1 = USDt * 365 / 31 / 3600, which is just the inverted formula above.
Now, to get the actual %, just replace the right hand side of the expression before by the actual values
APR = USDa * 365 / 31 / 3600, USDa = 3600 * 0.1 * 30 / 365, so
APR = 3600 * 0.1 * 30 / 365 * 365 / 31 / 3600 = 0.1*30/31, which is 0.0968. Sounds familiar right? this is 0.1 - 0.0032, where 0.0032 is the 3.2% calculated before, but multiplied by 0.1 because it's in APR terms.
0x73696d616f
Other points to consider:
- If there is an outage it does not matter who is the actor calling distribute yield.
- The chance is small for a big outage, but smaller to medium delays are much more likely (where smaller delays are guaranteed).
WangSecurity
Firstly, as I understand all of us agree that the vulnerability is present in the code. The problem here is that there is firstly, high constraints:
- Keepers not calling distribute yield.
- Users not calling distribute yield as well.
But there is no guarantee that the keeper will cetainly make a call and not misbehave and there is no guarantee that the user will call it on time. So I believe, it's fair to say that the daly may indeed happen, but with very low probability. To better understand what are the losses, @0x73696d616f can you make these calculations if it's only 1 hr delay or 5 min delay? And @panprog and @CergyK would like to ask you check the calculations above to verify they're 100% correct.
0x73696d616f
The calculations are very simple, the loss is delay / (30 days + delay) × earnings. One hour is 1 / (30x24 + 1) = 0.13%. 5 minutes is 5 / (30x24x60 + 5) = 0.01%. If the earnings are 360_000, the loss is 500 and 42 USD, respectively.
CergyK
The calculations are very simple, the loss is delay / (30 days + delay) × earnings. One hour is 1 / (30x24 + 1) = 0.13%. 5 minutes is 5 / (30x24x60 + 5) = 0.01%. If the earnings are 360_000, the loss is 500 and 42 USD, respectively.
Agree with these calculations
panprog
The calculations are very simple, the loss is delay / (30 days + delay) × earnings. One hour is 1 / (30x24 + 1) = 0.13%. 5 minutes is 5 / (30x24x60 + 5) = 0.01%. If the earnings are 360_000, the loss is 500 and 42 USD, respectively.
Yes, the calculations are correct.
WangSecurity
As I understand, the admin relies on keeper bots to call distributeYield, but as we agree there can be a delay and it's not guaranteed that they will call on time every time. Or am I missing something here?
0x73696d616f
@WangSecurity yes, that's it. I think the previous comment ignored a good chunk of this conversation.
ZdravkoHr
If this issue is deemed valid, #85 should also be validated, because:
- it has the same impact, but on a bigger scale - the delay is 30 days and it's certain.
- it does not rely on keeper being inactive - will happen for sure
0x73696d616f
@ZdravkoHr I don't think they should be grouped together, you'll notice I submitted a separate issue #345 which was exactly like #85 and was invalidated for the same reason (sponsor's intention for the first distribution to be 60 days). In #345 and #85, they both point at the fact that the initial distribution is 60 days, not 30. This issue goes a bit further and claims that beyond the initial period, there will be more delays as the keeper can not guarantee strictly calling it on time. This is further demonstrated by the recommendations given, using the one from #85, this issue (#347) would still exist. Thus, #85 does not identify the correct attack path.
ZdravkoHr
@0x73696d616f, I agree #85 doesn't talk about daysBetweenDistributions in more detail, but the root issue is still the same, as described
Therefore, the first distribution can be executed after at least 60 days since the unlocking have passed. However, the whole distribution process calculates its values for a single month, using daysBetweenDistributions.
If earningsTrancheuse was not fixated to 30 days, but instead it calculated time correctly, there would have been no problem with initial distribution happening after 60 days.
0x73696d616f
@ZdravkoHr I know you were aware of the issue when you wrote the report (as you correctly identified the root cause), but the Sherlock rules are clear, if the identified attack path is incorrect (as is the case in #85 as your whole report only mentioned the initial 60 days period), the issue is at most low.
ZdravkoHr
Both B & C would not have been possible if error A did not exist in the first place. In this case, both B & C should be put together as duplicates.
In addition to this, there is a submission D which identifies the core issue but does not clearly describe the impact or an attack path. Then D is considered low.
I don't think #85 is issue D in this scenario. There isn't really an attack path in this situation. There is a root cause which causes loss in yield based on a given delay. The delay in #85 is caused by executing the initial distribution 30 days later, while the delay in this issue is caused by a keeper being inactive.
WangSecurity
As we agree, the keeper might have a delay and not guaranteed to call on time. Secondly, despite it's being in the user interest to call it on time as well, it's not guaranteed as well. Thirdly, as was proven by the escalator the loss is equal or exceeds 0.01% which I believe exceeds small and finite amounts. Finally, as I understand, the sponsor said it's acceptable only after the contest and it wasn't known during the contest that this risk is known/acceptable.
Hence, planning to accept the escalation and validate the issue with medium severity.
ZdravkoHr
@WangSecurity, what do you think about #85?
0x73696d616f
#85 explicitly says the root cause is the initial period being 60 days. It mentions that the yield is calculated on 30 days, but does not see this part as the root cause.
RealLTDingZhen
Hey @WangSecurity @panprog after a careful reading, I believe this issue is valid, and should be duplicated with #697 #692 #672 #657.
Zivoe didn't tell watsons whether they were going to run a keeper themselves or use an external keeper. Lets assume for this issue users will not calling distribute yield.
If protocol is going to run a keeper themselves, then this is not a issue because admin/owner is TRUSTED.
If the external Keeper does not follow the rules for calling the distributeYield() on time, this is a misbehave of external admin, so this issue should be duplicated with those where external admin's actions are leading to a harm of Zivoe with Medium severity.
Are the admins of the protocols your contracts integrate with (if any) TRUSTED or RESTRICTED?
RESTRICTED
And please refer to here:
https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/672#issuecomment-2115546327
according to README and external admins being restricted, these reports indeed should be valid. But, I believe the most fair option is to duplicate this report with other issues where external admin's actions are leading to a harm of Zivoe with Medium severity, due to extremely low likelihood.
0x73696d616f
Lets assume for this issue users will not calling distribute yield.
This assumption makes no sense.
If protocol is going to run a keeper themselves, then this is not a issue because admin/owner is TRUSTED.
This is false as they can not control network congestion.
RealLTDingZhen
If this assumption makes no sense, this issue should be invalidated😂 If users will call distribute yield immediately, this is not a issue because sponsor confirms
It will called immediately when possible, so there may be few blocks of lag, acceptable
Please refer to https://docs.sherlock.xyz/audits/judging/judging#vii.-list-of-issue-categories-that-are-not-considered-valid
#20: Chain re-org and network liveness related issues are not considered valid. Exception: If an issue concerns any kind of a network admin (e.g. a sequencer), can be remedied by a smart contract modification, the protocol team considers external admins restricted and the considered network was explicitly mentioned in the contest README, it may be a valid medium. It should be assumed that any such network issues will be resolved within 7 days, if that may be possible.
This issue is not a vulnerability if one of keepers or protocol team or users is behave normally. In your words, it is a issue because of network congestion, which is not accepted by sherlock.
The only possible path for this issue is a external Keeper bot misbehave, which should be duped with those issues above.
0x73696d616f
If this assumption makes no sense, this issue should be invalidated😂 If users will call distribute yield immediately, this is not a issue because sponsor confirms
network congestion.
A small delay is not considered 'network liveness', it's regular behaviour. And the issue may be fixed by a smart contract modification.
RealLTDingZhen
As you agree there may be a small delay:
https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/347#issuecomment-2081539634
there may be few blocks of lag, acceptable
So, what kind of network congestion(not a issue with network liveness) can make this a issue? If this congestion occurs on the Keeper side,
Per LSW:https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/347#issuecomment-2114623235
A mainnet outage event of which 2 happened last year, would have to coincide exactly with the beginning of a distributeYield unlocking, which is quite unlikely (1 hour / 30 days = 0.01% chance of overlap)
This is definitly low likelihood + low impact(keepers should call the function right after the congestion, so only a few blocks of lag)
0x73696d616f
@RealLTDingZhen the numbers are all above, please refer to them.
RealLTDingZhen
I see your calculations ser, but this did not counter my(and Cergyk) point.
the loss is delay / (30 days + delay) × earnings
I agree that the losses will increase in that proportion, but this do not counter my arguments.
My two main arguments are:
If we assume this issue do not require a malicious Keeper and protocol team/users are all not aware of this, then:
- the likelihood is still extremely low. The "network congestion" have to coincide exactly with the beginning of a distributeYield unlocking. (@CergyK can you please confirm how long the 2 mainnet outage event in last year last?)
- This issue relays on a long Ethereum mainnet downtime. As you have calculated, 1 hour lag = 0.13% loss of yield that month. However, an hour of downtime on Ethereum mainnet can definitely be seen as a network liveness issue. (Consider those lending/perpetual/oracle protocols, it would be disastrous for them.) If the lag is just some blocks, sponsor is fully acknowledged and can accept the risk, so this is a informational issue.
0x73696d616f
The likelihood goes from very high to low as the duration of the delay goes up. You're picking on the upper bound and basing the argument on it, which is deceiving.
RealLTDingZhen
If the network is congested for a long period of time, then this issue can be invalidated outright by reason "network liveness". If the network is congested for a short period of time, then this issue can be invalidated outright because sponsor confirms there may be few blocks of lag, and that is acceptable.
So how long is the right delay to make this issue both probable and impactful? And my points above is still valid to invalidate this issue.
0x73696d616f
There is no clear line as to where the delay stops being considered acceptable. It's up to the user deciding the escalations to decide, which has been done.
this issue can be invalidated outright because sponsor confirms there may be few blocks of lag, and that is acceptable.
This has also been answered.
RealLTDingZhen
This is my final statement on this issue (Summary of above views):
-
If we think this issue need a malicious/misbehave Keeper bot, then this should be duped with #697 #692 #672 #657.
-
If we think this issue can be triggered by normal operation, then this issue should be invalidated for reasons below: a. The likelihood is extremely low: A mainnet outage event of which 2 happened last year, would have to coincide exactly with the beginning of a distributeYield unlocking, which is quite unlikely. And even if such coincidence downtime exists, the issue becomes a network liveness issue because the loss is proportional to the downtime(5min -> 1BIPS, which is a network liveness issue.). b. The impact is low: 5min downtime in Ethereum-> 1 BIPS loss itself is low impact. and sponsor confirms that "there may be few blocks of lag, acceptable".
RealLTDingZhen
Let's leave it up to the judge for now, it's ultimately up to him. Good discussion @0x73696d616f I always like a good debate 👍
0x73696d616f
If we think this issue can be triggered by normal operation, then this issue should be invalidated for reasons below: a. The likelihood is extremely low: A mainnet outage event of which 2 happened last year, would have to coincide exactly with the beginning of a distributeYield unlocking, which is quite unlikely. And even if such coincidence downtime exists, the issue becomes a network liveness issue. b. The impact is low: 5min downtime in Ethereum downtime -> 1 BIPS loss itself is low impact. and sponsor confirms that "there may be few blocks of lag, acceptable".
Your message is incorrectly implicitly mixing the likelihood of the 1 hour event with the impact of a 5 minutes delay, Also, please don't mention point 1. as this was never mentioned in this report, you're just adding confusion.
RealLTDingZhen
Your message is incorrectly implicitly mixing the likelihood of the 1 hour event with the impact of a 5 minutes delay, Also, please don't mention point 1. as this was never mentioned in this report, you're just adding confusion.
This is because your issue requires distribute yield function not be called for a period of time, and there are only two scenarios for this - Keeper misbehave or network downtime.
WangSecurity
I hope this will answer your questions:
Firstly, there is no guarantee the keeper will work without a delay. Secondly, if we consider it external, I believe it's not a dup of #657 because 657 requires external admins executing certain actions to completely rug Zivoe (either block their functionality or steal funds), while this can happen at any time, cause it's not guaranteed to execute every call on time every time. Thirdly, I see the sponsor saying it's acceptable, but it's said only after the contest and wasn't known during it. Fourthly, I agree that the loss is very small, and the likelihood as well. But it still qualifies for medium severity: high external constraints and the loss of funds exceeds small and finita amounts. I believe 0.01% exceeds small and finite amounts. If any of my assupmtions is wrong, please correct me or ask questions.
But the decision remains the same, accept the escalation and validate the report with medium severity.
ZdravkoHr
@0x73696d616f, I agree in my report I have described setting the lastDistributed as a root cause. But I do still think that #85 deserves revisiting, even if it's not judged as a duplicate of this issue because the impact is the same if not worse
Evert0x
Result: Medium Unique
sherlock-admin4
Escalations have been resolved successfully!
Escalation status:
- 0x73696d616f: accepted
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Zivoe. Sherlock made it public on 12 May 2024. Found in the 280 audit contest (finding M-13).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.