Re-enterable Code When Making a Deposit to Stake
The deposit function in the staking contract updates the user's balance and reward debt only after transferring reward tokens, so if the reward token gave control back during the transfer an attacker could re-enter deposit and receive the same pending reward repeatedly. The author states this could drain the contract's entire rJoe balance, but notes the current token does not relinquish control, so it is not exploitable today.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by 0v3rf10w and static
Note: this attack requires rJoe to relinquish control during tranfer() which under the current RocketJoeToken it does not. Thus this vulnerability is raised as medium rather than high. Although it’s not exploitable currently, it is a highly risky code pattern that should be avoided.
This vulnerability would allow the entire rJoe balance to be drained from the contract.
Proof of Concept
The function deposit() would be vulnerable to reentrancy if rJoe relinquished control flow.
The following lines show the reward calculations in variable pending. These calculations use two state variables user.amount and user.rewardDebt. Each of these are updated after _safeRJoeTransfer().
Thus if an attacker was able to get control flow during the rJoe::tranfer() function they would be able to reenter deposit() and the value calculated for pendingwould be the same as the previous iteration hence they would again be transferred pending rJoe tokens. During the rJoe transfer the would again gain control of the execution and call deposit() again. The process could be repeated until the entire rJoe balance of the contract has been transferred to the attacker.
if (user.amount > 0) {
uint256 pending = (user.amount * accRJoePerShare) /
PRECISION -
user.rewardDebt;
_safeRJoeTransfer(msg.sender, pending);
}
user.amount = user.amount + _amount;
user.rewardDebt = (user.amount * accRJoePerShare) / PRECISION;
Recommended Mitigation Steps
There are two possible mitigations. First is to use the openzeppelin reentrancy guard over the deposit() function which will prevent multiple deposits being made simultaneously.
The second mitigation is to follow the checks-effects-interactions pattern. This would involve updating all state variables before making any external calls.
cryptofish7 (Trader Joe) confirmed, but disagreed with severity and commented:
Disagree with severity
I agree with the warden’s assessment of risk on this one. Leaving it unaddressed would represent a potential future compromise if it was forgotten about by the team.
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
The contract performs the external token transfer before updating user.amount and user.rewardDebt, so a re-entrant call would recompute the same pending reward.
Impact
If the reward token ever relinquished control during transfer, an attacker could drain the contract's entire rJoe balance; under the current token this is not exploitable, which is why it was rated medium.
What the attacker needed
Reward token must give control back mid-transfer; attacker holds a staking position with pending rewards
The fix
The deposit function should be protected with a reentrancy guard or restructured to follow checks-effects-interactions by updating state before the external transfer; the report notes a fix pull request was opened.
How it was found
- Call deposit with a non-zero existing user.amount so a pending reward is calculated.
- During the reward token transfer, regain control of execution.
- Re-enter deposit before user.amount and user.rewardDebt are updated.
- Repeat the re-entry until the contract's rJoe balance is drained.
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 reentrancy bug happens when a contract sends tokens to someone before it finishes updating its own records. That person can call back into the same function while the records are still old, so the contract pays out again based on the same numbers. Here the reward token would need to hand control back mid-transfer for this to work, and the report says the current token does not do that.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.