ConvexMasterChef ’s deposit and withdraw can be reentered drawing all reward funds from the contract if reward token allows for transfer flow control
The deposit and withdraw functions update reward accounting after transferring rewards, allowing reentrancy if the reward token permits transfer flow control. An attacker could repeatedly reenter with a tiny amount to drain all reward tokens from the contract.
Submitted by hyh
Reward token accounting update in deposit() and withdraw() happens after reward transfer. If reward token allows for the control of transfer call flow or can be upgraded to allow it in the future (i.e. have or can introduce the _beforetokentransfer, _afterTokenTransfer type of hooks; or, say, can be upgraded to ERC777), the current implementation makes it possible to drain all the reward token funds of the contract by directly reentering deposit() or withdraw() with tiny _amount.
Setting the severity to medium as this is conditional to transfer flow control assumption, but the impact is the full loss of contract reward token holdings.
Proof of Concept
Both withdraw() and deposit() have the issue, performing late accounting update and not controlling for reentrancy:
ConvexMasterChef.sol#L209-L221
function deposit(uint256 _pid, uint256 _amount) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
updatePool(_pid);
if (user.amount > 0) {
uint256 pending = user
.amount
.mul(pool.accCvxPerShare)
.div(1e12)
.sub(user.rewardDebt);
safeRewardTransfer(msg.sender, pending);
}
pool.lpToken.safeTransferFrom(
ConvexMasterChef.sol#L239-L250
function withdraw(uint256 _pid, uint256 _amount) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
require(user.amount >= _amount, "withdraw: not good");
updatePool(_pid);
uint256 pending = user.amount.mul(pool.accCvxPerShare).div(1e12).sub(
user.rewardDebt
);
safeRewardTransfer(msg.sender, pending);
user.amount = user.amount.sub(_amount);
user.rewardDebt = user.amount.mul(pool.accCvxPerShare).div(1e12);
pool.lpToken.safeTransfer(address(msg.sender), _amount);
Recommended Mitigation Steps
Consider adding a direct reentrancy control, e.g. nonReentrant modifier:
https://docs.openzeppelin.com/contracts/2.x/api/utils#ReentrancyGuard
Also, consider finishing all internal state updates prior to external calls:
0xMaharishi (Aura Finance) confirmed and commented:
Protected by governance, but agree could be solved with simple reentrancy guard.
0xMaharishi (Aura Finance) resolved:
code-423n4/2022-05-aura#6 code4rena aurafinance/aura-contracts#84
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 accounting state (user.rewardDebt and user.amount) is updated after the external reward transfer, and no reentrancy guard is used.
Impact
If the reward token allows transfer flow control, an attacker could drain all reward token funds from the contract; the report rates severity as medium because this is conditional on that assumption.
What the attacker needed
Reward token allows transfer flow control; attacker can call deposit or withdraw
The fix
The project added a reentrancy guard and moved state updates before external calls, as confirmed by the team.
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 smart contract, a reentrancy attack happens when a function calls an external contract before finishing its own bookkeeping. That external contract can call back into the original function, which still sees old balances, and repeat the process to steal funds. Here, the reward transfer happens before the user's reward debt is updated, so a malicious token could exploit this.