ZivoeYDL::distributeYield yield distribution is flash-loan manipulatable
The yield distribution function calculates each recipient's share using the current total supply of staked and vesting tokens, which can be temporarily inflated by a large stake just before the function is called. An attacker could use a flash loan to stake a large amount, call the distribution function to increase the staked token's share of rewards, then unstake and sell back the tokens, thereby diverting rewards from vesting token holders.
Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/298
The protocol has acknowledged this issue.
Found by
cergyk
Summary
ZivoeYDL::distributeYield is used to "distributes available yield within this contract to appropriate entities" but it relies on the tokens totalSupply() which can be manipulable through a flashloan.
Vulnerability Detail
ZivoeYDL::distributeYield relies on stZVE().totalSupply() to distribute protocol earnings and residual earnings:
// Distribute protocol earnings.
...
else if (_recipient == IZivoeGlobals_YDL(GBL).stZVE()) {
uint256 splitBIPS = (
>> IERC20(IZivoeGlobals_YDL(GBL).stZVE()).totalSupply() * BIPS
) / (
IERC20(IZivoeGlobals_YDL(GBL).stZVE()).totalSupply() +
IERC20(IZivoeGlobals_YDL(GBL).vestZVE()).totalSupply()
);
...
// Distribute residual earnings.
...
else if (_recipient == IZivoeGlobals_YDL(GBL).stZVE()) {
uint256 splitBIPS = (
>> IERC20(IZivoeGlobals_YDL(GBL).stZVE()).totalSupply() * BIPS
) / (
IERC20(IZivoeGlobals_YDL(GBL).stZVE()).totalSupply() +
IERC20(IZivoeGlobals_YDL(GBL).vestZVE()).totalSupply()
);
...
This can be abused by an attacker by buying then staking a very large amount of ZVE right before calling ZivoeYDL::distributeYield (a flashloan can be used) in order to game the system and collect a lot more distributed yields than he should be entitled to.
Scenario
- Attacker buys and stakes a very large amount of
ZVEthrough a flashloan - Attacker calls
ZivoeYDL::distributeYield - Attacker collects a very large amount of distributed yields
- Attacker withdraws and sells back his
ZVEtokens effectively stealing undeserved yields
Impact
A user can systematically claim a big chunk of the rewards reserved to stZVE and vestZVE in ZivoeYDL by using a flash loan
Code Snippet
Tool used
Manual Review
Recommendation
Use a minimal time locking for stZVE such as it would not be possible to stake and unstake all in one block, or use past (1 block in the past) total supply to claim rewards
Discussion
pseudonaut
Not valid
sherlock-admin4
1 comment(s) were left on this issue during the judging contest.
panprog commented:
medium. The scenario presented isn't actually possible, because staking reward is deposited to be distributed over the long period of time, so the attacker won't earn anything from staking for 0 blocks. However, it still allows to manipulate the ratio of stZVE/vestZVE reward allocation, so it's possible to inflate reward to stZVE, then withdraw, but stZVE will receive inflated reward (where attacker can have a significant amount of token staked to benefit from increased yield). This is at the expense of vestZVE which will receive deflated reward.
panprog
Keeping this medium.
While the scenario is not possible as described, it still describes a valid issue, attack vector and core reason (manipulation to inflate stZVE's share of distribution).
panprog
Sponsor response:
there's no economically feasible way to make this occur, given restricted circulating supply (<10% circulating for a number of years, with 30% in DAO and 60% in vestZVE, slippage/fees on DEXs and likely supply lowered due to CEX trading) and it implies that user maintains amount being staked after the flash-loan (separate from whatever was used to manipulate it) and doesn't even account for other users coming in over next 30 days and diluting any additional yield earned
panprog
You don't need a lot of ZVE to impact the reward distribution. For example, the ratio is 1% in stZVE and 99% in vestZVE. If you buy 1% of ZVE, stake it, call distributeYield, then unstake and sell back ZVE, the ratio is now 2% in stZVE, 98% in vestZVE, so a double rate for stZVE for the next 30 days. The loss for vestZVE is not large, but still loss nonetheless.
Keeping this medium.
0x73696d616f
Escalate
The issue described should be invalid as it is the intended behaviour of the protocol. Since the user has to stake to get the rewards for some time, he would have done the regular operation for users. The user is not getting more rewards for himself than any user that legitimately staked before. He is just staking closer to the deadline. Keep in mind that the protocol is always handing out the same APR (unless not enough liquidity is available) to all stakers, so in practice other users would not even lose from this.
sherlock-admin3
Escalate
The issue described should be invalid as it is the intended behaviour of the protocol. Since the user has to stake to get the rewards for some time, he would have done the regular operation for users. The user is not getting more rewards for himself than any user that legitimately staked before. He is just staking closer to the deadline. Keep in mind that the protocol is always handing out the same APR (unless not enough liquidity is available) to all stakers, so in practice other users would not even lose from this.
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
The issue is that a 1-transaction inflated staked amount allows to inflate stakers distribution at the loss of vesters distribution.
WangSecurity
I'm not sure I understand how it effects the yield after we call distributeYield. If we take example from this comment:
The ratio is 1% stZVE and 99% vstZVE. The attacker stakes ZVE, so it's now 2% for stZVE, call disitrbute yield, then unstake ZVE, so it's again 1%.
The timelock to call distributeYield passes and we call it again. We firstly calculate the ratio, which will be 1% stZVE and 99% vstZVE (I think I miss something here) and then the yield is distributed. So the main question is are these inflated ratios (2% for stZVE) actually saved upon the next time we call distributeYield?
Or the problem is that we can front-run the call to ditributeYield every time to get more rewards?
(Sorry if silly questions, but the phrase "double rate for stZVE for the next 30 days" really confuses me, since the ratio is calculated right before transfering the rewards)
0x73696d616f
Hey @WangSecurity,
The ratio is 1% stZVE and 99% vstZVE. The attacker stakes ZVE, so it's now 2% for stZVE, call disitrbute yield, then unstake ZVE, so it's again 1%.
distributeYield is only called every 30 days. The yield during these 30 days is distributed based on a snapshot of the total supplies at the time distributeYield is called. This issue claims that if you take a flashloan of ZVE before calling distributeYield and stake, increasing the stZVE supply, the snapshot is taken, and the % of yield attributed to stZVE for the 30 days is increased. After 30 days, another distributeYield call happens and a snapshot is taken again, where the % is back to 1% as the flashloanee would have unstaked the stZVE by then.
My claim is that this is the intended behaviour, the flashloan does not matter here at all because the attacker has to stake to profit from this. If the attacker stakes, then he also rightfully so receives rewards for the increased supply of stZVE.
panprog
@WangSecurity
distributeYield sets the rate for the next 30 days.
Scenario A: 1% stZVE, 99% vstZVE. distributeYield sets the rate for the next 30 days: stZVE get $1/second, vstZVE get $99/second.
Scenario B: 1% stZVE, 99% vstZVE. User acquires 1% ZVE, stakes it, calls distributeYield, unstakes, returns back ZVE. The rate set for the next 30 days: stZVE get $2/second, vstZVE get $98/second.
In Scenario B, stZVE stakers get double the rate they should while ZVE is released into circulation as if it was never staked. Basically, it's possible to inflate stZVE reward rate while not really staking corresponding ZVE amount.
0x73696d616f
The design is suboptimal but it is ultimately a design choice. It can not be gamed like this issue says with a flashloan. The supposed attack is just regular user operation. I believe in these cases it is not a valid medium on Sherlock, but I leave it up to the judge.
WangSecurity
I see and understand with arguments both for and against it being the designed decision. But this in fact allows to inflate the stZVE rewards without actually staking ZVE. In the edge case, the attack can be done in one transaction and inflate the rewards in several times (even more than 2x as in the Lead Judge's example). Hence, I believe it's indeed valid, planning to reject the escalation and leave the issue as it is.
RealLTDingZhen
I agree with @0x73696d616f . It should be a design choice here, because this issue cannot be fixed in current protocol. Even if a minimum holding interval is guaranteed, whales can still change the stZVE/vestZVE reward distribution ratio by
Attacker buys and stakes a very large amount of ZVE through a flashloan in advance Attacker wait for minimum holding interval Attacker calls ZivoeYDL::distributeYield Attacker collects a very large amount of distributed yields Attacker withdraws and sells back his ZVE tokens effectively stealing undeserved yields
The only way to fix this is to add a withdraw fee, which means a redesign of protocol core logic. So I believe this is not a contract vulnerablilty.
WangSecurity
Thank you for that response! Firstly, I don't think that the mitigation is the reason to validate/invalidate the issue, moreover, the LSW provided two other mitigations. Secondly, I understand that the same can happen with regular workflow, but I believe it's still an issue, cause allows the attacker to intentionally manipulate the distribution ratio without actually staking ZVE (only for one block). Hence, my decision remains the same.
Evert0x
Result: Medium Unique
sherlock-admin4
Escalations have been resolved successfully!
Escalation status:
- 0x73696d616f: rejected
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 current total supply in reward share calculation allows manipulation by temporarily changing the supply.
Impact
A user could systematically claim a big chunk of the rewards reserved for stZVE and vestZVE by using a flash loan, although the sponsor argues it is not economically feasible due to restricted circulating supply and other factors.
What the attacker needed
Access to a flash loan; ability to stake ZVE; call distributeYield
The fix
The recommendation is to use a minimal time locking for stZVE so that staking and unstaking cannot occur in one block, or to use past total supply (from one block ago) to claim rewards.
How it was found
- Attacker buys and stakes a very large amount of ZVE through a flashloan
- Attacker calls ZivoeYDL::distributeYield
- Attacker collects a very large amount of distributed yields
- Attacker withdraws and sells back his ZVE tokens effectively stealing undeserved yields
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.
The protocol distributes rewards based on how many tokens are staked at the moment the distribution function runs. An attacker can temporarily stake a huge amount using a flash loan (a loan that is borrowed and repaid in the same transaction) to make it look like more tokens are staked, which increases the staked group's share of rewards. After the distribution, the attacker unstakes and repays the loan, keeping the extra rewards.
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.