Wrong reward distribution between early and late depositors because of the late syncRewards() call in the cycle, syncReward() logic should be executed in each withdraw or deposits (without reverting)
The reward distribution function syncRewards() is only called manually, so if it is delayed, users who deposit or withdraw before it is called lose their share of rewards, which instead go to later depositors. An attacker could time a withdrawal to avoid forfeiting rewards, but the text does not describe a concrete attack.
unforgiven Follow with Bnke0x0, joestakey and 15 others
Originally disclosed on Code4rena
Submitted by unforgiven, also found by Bnke0x0, joestakey, 0xNazgul, Breeje, IllIllI, IllIllI, 141345, kiki_dev, koxuan, fs0c, ck, btk, csanuragjain, rvierdiiev, HollaDieWaldfee, 0x73696d616f, and Rolezn
https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/TokenggAVAX.sol#L88-L109 https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/TokenggAVAX.sol#L166-L178 https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/TokenggAVAX.sol#L180-L189
Function syncRewards() distributes rewards to TokenggAVAX holders, it linearly distribute cycle’s rewards from block.timestamp to the cycle end time which is next multiple of the rewardsCycleLength (the end time of the cycle is defined and the real duration of the cycle changes). when a cycle ends syncRewards() should be called so the next cycle starts but if syncRewards() doesn’t get called fast, then users depositing or withdrawing funds before call to syncRewards() would lose their rewards and those rewards would go to users who deposited funds after syncRewards() call. contract should try to start the next cycle whenever deposit or withdraw happens to make sure rewards are distributed fairly between users.
Proof of Concept
This is syncRewards() code:
function syncRewards() public {
uint32 timestamp = block.timestamp.safeCastTo32();
if (timestamp < rewardsCycleEnd) {
revert SyncError();
}
uint192 lastRewardsAmt_ = lastRewardsAmt;
uint256 totalReleasedAssets_ = totalReleasedAssets;
uint256 stakingTotalAssets_ = stakingTotalAssets;
uint256 nextRewardsAmt = (asset.balanceOf(address(this)) + stakingTotalAssets_) - totalReleasedAssets_ - lastRewardsAmt_;
// Ensure nextRewardsCycleEnd will be evenly divisible by `rewardsCycleLength`.
uint32 nextRewardsCycleEnd = ((timestamp + rewardsCycleLength) / rewardsCycleLength) * rewardsCycleLength;
lastRewardsAmt = nextRewardsAmt.safeCastTo192();
lastSync = timestamp;
rewardsCycleEnd = nextRewardsCycleEnd;
totalReleasedAssets = totalReleasedAssets_ + lastRewardsAmt_;
emit NewRewardsCycle(nextRewardsCycleEnd, nextRewardsAmt);
}
As you can see whenever this function is called it starts the new cycle and sets the end of the cycle to the next multiple of the rewardsCycleLength and it release the rewards linearly between current timestamp and cycle end time. So if syncRewards() get called near to multiple of the rewardsCycleLength then rewards would be distributed with higher speed in less time. The problem is that users depositing funds before call syncRewards() won’t receive new cycles rewards and early depositing won’t get considered in reward distribution if deposits happen before syncRewards() call and if a user withdraws his funds before the syncRewards() call then he receives no rewards.
Imagine this scenario:
rewardsCycleLengthis 10 days and the rewards for the next cycle is100AVAX.- the last cycle has been ended and user1 has
10000AVAX deposited and has 50% of the pool shares. syncRewards()don’t get called for 8 days.- users1 withdraws his funds receive
10000AVAX even so he deposits for 8 days in the current cycle. - users2 deposit
1000AVAX and get 10% of pool shares and the user2 would callsyncRewards()and contract would start distributing100avax as reward. - after 2 days cycle would finish and user2 would receive
100 * 10% = 10AVAX as rewards for his1000AVAX deposit for 2 days but user1 had10000AVAX for 8 days and would receive 0 rewards.
So rewards won’t distribute fairly between depositors across the time and any user interacting with contract before the syncRewards() call can lose his rewards. Contract won’t consider deposit amounts and duration before syncRewards() call and it won’t make sure that syncRewards() logic would be executed as early as possible with deposit or withdraw calls when a cycle ends.
Tools Used
VIM
Recommended Mitigation Steps
One way to solve this is to call syncRewards() logic in each deposit or withdraw and make sure that cycles start as early as possible (the revert “SyncError()” in the syncRewards() should be removed for this).
emersoncloud (GoGoPool) disagreed with severity and commented:
I think that medium severity is more appropriate. User funds aren’t drained or lost, but liquid staking rewards may be unfairly calculated.
Good find. I think there is something we could do to either incentivize the
syncRewardscall or call it on deposit and withdraw.But I disagree with the concept that the user who staked for 8 days is entitled to rewards for staking. Rewards depend on properly running minipools. Reward amounts can fluctuate depending on the utilization of liquid staking funds by minipools.
emersoncloud (GoGoPool) commented:
After some more discussion, this is working as designed. And Rialto will call
syncRewardsat the start of each reward cycle, so the possible loss to the user who withdraws beforesyncRewardswas supposed to be called is mitigated.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The Warden has shown a potential risk for end-users that withdraw before
syncRewardsis called.Because the finding pertains to a loss of yield, I believe the finding to be of Medium Severity.
While Rialto may call this as a perfect actor, we cannot guarantee that a end user could forfeit some amount of yield, due to external conditions.
I believe this finding to potentially be a nofix, as long as all participants are aware of the mechanic.
Per discussions had on #99, I don’t believe that any specific MEV attack has been identified, however this finding does highlight a potential risk that a mistimed withdrawal could cause.
emersoncloud (GoGoPool) commented:
Acknowledged.
Not fixing but will add a note in our docs.
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 reward cycle is only started by a manual syncRewards() call, so deposits and withdrawals made before that call are not accounted for in the new cycle's reward distribution.
Impact
Users who withdraw before syncRewards() is called could forfeit their yield, and rewards may be unfairly distributed; the project stated that Rialto will call syncRewards at the start of each cycle, mitigating the loss.
What the attacker needed
SyncRewards() is not called promptly at cycle end; user deposits or withdraws before syncRewards() is called
The fix
The project acknowledged the issue but did not fix it, planning to add a note in the documentation instead.
How it was found
- Set rewardsCycleLength to 10 days and next cycle rewards to 100 AVAX.
- User1 deposits 10000 AVAX and holds 50% of pool shares.
- syncRewards() is not called for 8 days.
- User1 withdraws all funds and receives no rewards for the 8 days.
- User2 deposits 1000 AVAX, gets 10% shares, and calls syncRewards().
- After 2 days, user2 receives 10 AVAX as rewards while user1 received nothing.
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 staking pool, rewards are shared based on how long and how much you deposit. The pool uses a function to start a new reward period, but if that function is called late, people who deposited or withdrew before the call miss out on rewards. This is unfair but does not steal funds directly.
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.