Locking rewards tokens in Staking contract when there are no stakes
The Staking contract does not update its reward accounting when total staked supply is zero, so rewards sent during such periods become permanently locked. An attacker could cause this by ensuring no tokens are staked when rewards are claimed, though the report notes the situation is unlikely.
Submitted by Haipls, also found by Cyfrin and ast3ros
Impact
Blocking rewards assigned to stakes from the sale of lottery tickets when stakes are absent in the Staking contract.
So, the situation is unlikely, but it can happen.
Proof of Concept
In order to confirm the problem, we need to prove 2 things:
- That there may be no staked tokens -> #Total supply == 0
- That the reward that will be transferred to the contract will be blocked -> #Locked tokens
Total supply == 0:
Flow 1:
- Deploy protocol
- Anyone buys tickets
- Anyone calls claimRewards() for
LotteryRewardType.STAKING - Since no one has managed to stake the tokens yet, the reward for the first sold tickets will be transferred to the staking contract and blocked
- First user stakes tokens and we can see the Staking contract will have rewards but for first user
rewardPerTokenwill start from0andlastUpdateTicketIdwill update to actually
src/staking/Staking.sol
50: if (_totalSupply == 0) { // totalSupply == 0
51: return rewardPerTokenStored;
120: rewardPerTokenStored = currentRewardPerToken; // will set 0
121: lastUpdateTicketId = lottery.nextTicketId(); // set actually
- Tokens before the first stake will be blocked forever
Flow 2:
- Everything is going well. The first bets are made before the sale of the first tokens
- But there will moments when all the stakers withdraw their bets
- Get time periods when
totalSupply == 0 - During these periods, all the rewards that will come will be blocked on the contract by analogy with the first flow.
Locked tokens
- Tokens are blocked because
rewardPerTokenStoredis not updated whentotalSupply == 0
Let’s just write a test:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./LotteryTestBase.sol";
import "../src/Lottery.sol";
import "./TestToken.sol";
import "forge-std/console2.sol";
import "../src/interfaces/ILottery.sol";
contract StakingTest is LotteryTestBase {
IStaking public staking;
address public constant STAKER = address(69);
ILotteryToken public stakingToken;
function setUp() public override {
super.setUp();
staking = IStaking(lottery.stakingRewardRecipient());
stakingToken = ILotteryToken(address(lottery.nativeToken()));
}
function testDelayFirstStake() public {
console.log(
"Init state totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
buySameTickets(lottery.currentDraw(), uint120(0x0F), address(0), 4);
lottery.claimRewards(LotteryRewardType.STAKING);
console.log(
"After buy tickets totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
vm.prank(address(lottery));
stakingToken.mint(STAKER, 1e18);
vm.startPrank(STAKER);
stakingToken.approve(address(staking), 1e18);
staking.stake(1e18);
console.log(
"After user stake, totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
// buy one ticket and get rewards
buySameTickets(lottery.currentDraw(), uint120(0x0F), address(0), 1);
lottery.claimRewards(LotteryRewardType.STAKING);
console.log(
"After buy tickets, totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
staking.exit();
console.log(
"After user exit, totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
buySameTickets(lottery.currentDraw(), uint120(0x0F), address(0), 1);
lottery.claimRewards(LotteryRewardType.STAKING);
console.log(
"After buy ticket again, totalSupply: %d, rewards: %d, rewardPerTokenStored: %d",
staking.totalSupply(),
rewardToken.balanceOf(address(staking)),
staking.rewardPerTokenStored()
);
}
}
Result:
Logs:
Init state totalSupply: 0, rewards: 0, rewardPerTokenStored: 0
After buy tickets totalSupply: 0, rewards: 4000000000000000000, rewardPerTokenStored: 0
After user stake, totalSupply: 1000000000000000000, rewards: 4000000000000000000, rewardPerTokenStored: 0
After buy tickets, totalSupply: 1000000000000000000, rewards: 5000000000000000000, rewardPerTokenStored: 0
After user exit, totalSupply: 0, rewards: 4000000000000000000, rewardPerTokenStored: 1000000000000000000
After buy ticket again, totalSupply: 0, rewards: 5000000000000000000, rewardPerTokenStored: 1000000000000000000
Recommended Mitigation Steps
One of:
- Develop a flow where funds should go in case of lack of stakers
- Develop a method of withdrawing blocked coins (which will not be distributed among stakers)
cccz (judge) decreased severity to Medium
rand0c0des (Wenwin) acknowledged
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-per-token calculation returns early when total supply is zero, so incoming rewards are not accounted for and remain locked.
Impact
Rewards assigned to staking can become permanently locked in the contract when no tokens are staked, though the report notes the situation is unlikely.
What the attacker needed
No tokens staked in the Staking contract; rewards claimed for staking during that period
The fix
The contract should be changed so that rewards sent while total supply is zero are either redirected or made withdrawable, rather than left locked.
How it was found
- Deploy the protocol and buy lottery tickets.
- Call claimRewards for the staking reward type while no tokens are staked.
- Observe that the reward tokens are transferred to the staking contract but rewardPerTokenStored remains zero.
- Stake tokens as the first user and note that the earlier rewards are not reflected in rewardPerTokenStored.
- Exit the stake and buy more tickets, then claim rewards again to see that new rewards are also locked.
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.
A staking contract holds tokens people deposit and pays them rewards over time. It tracks rewards per token, but if nobody has deposited yet, that tracker stays at zero. Any rewards sent while the tracker is zero get stuck in the contract and can never be paid out.
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.