Uninitialized RocketJoeStaking.lastRewardTimestamp can inflate rJoe supply
The RocketJoeStaking contract's lastRewardTimestamp is uninitialized, so if someone transfers Joe tokens directly to the contract before the first updatePool call, a large amount of rJoe is minted to the contract. This inflates the rJoe total supply, though the tokens are not distributed to users.
Submitted by cmichel
The RocketJoeStaking.lastRewardTimestamp is initialized to zero. Usually, this does not matter as updatePool is called before the first deposit and when joeSupply = joe.balanceOf(address(this)) == 0, it is set to the current time.
function updatePool() public {
if (block.timestamp <= lastRewardTimestamp) {
return;
}
uint256 joeSupply = joe.balanceOf(address(this));
// @audit lastRewardTimestamp is not initialized. can send 1 Joe to this contract directly => lots of rJoe minted to this contract
if (joeSupply == 0) {
lastRewardTimestamp = block.timestamp;
return;
}
uint256 multiplier = block.timestamp - lastRewardTimestamp;
uint256 rJoeReward = multiplier * rJoePerSec;
accRJoePerShare =
accRJoePerShare +
(rJoeReward * PRECISION) /
joeSupply;
lastRewardTimestamp = block.timestamp;
rJoe.mint(address(this), rJoeReward);
}
However, if a user first directly transfers Joe tokens to the contract before the first updatePool call, the block.timestamp - lastRewardTimestamp = block.timestamp will be a large timestamp value and lots of rJoe will be minted (but not distributed to users). Even though they are not distributed to the users, inflating the rJoe total supply might not be desired.
Recommendation
Consider tracking the actual total deposits in a storage variable and using this value instead of the current balance for joeSupply. This way, transferring tokens to the contract has no influence and depositing through deposit first calls updatePool and initializes lastRewardTimestamp.
cryptofish7 (Trader Joe) confirmed and commented:
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 contract uses its current token balance to calculate rewards without initializing the last reward timestamp, so a direct token transfer before the first updatePool call causes a huge reward mint.
Impact
The rJoe total supply could be inflated, which might not be desired, although the minted tokens are not distributed to users.
What the attacker needed
Ability to transfer Joe tokens to the contract
The fix
The fix was to track actual total deposits in a storage variable and use that instead of the current balance for joeSupply, so direct transfers have no influence.
How it was found
- Transfer Joe tokens directly to the RocketJoeStaking contract before any updatePool call.
- The next updatePool call sees a non-zero Joe balance and calculates rewards using the uninitialized lastRewardTimestamp, minting a large amount of rJoe to the contract.
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 is a smart contract that rewards people for staking tokens. It uses a timestamp to calculate rewards, but that timestamp starts at zero. If someone sends tokens directly to the contract before anyone stakes, the contract thinks a huge amount of time has passed and creates a huge number of reward tokens, which sit in the contract and increase the total supply.
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.