Wrong accounting logic when syncRewards() is called within beforeWithdraw makes withdrawals impossible
The sfrxETH vault's beforeWithdraw function calls syncRewards after decrementing storedTotalAssets but before the assets are transferred out, so the reward calculation uses an inflated balance. This can cause excessive rewards to be paid out and later syncRewards calls to underflow, making withdrawals impossible.
Submitted by Lambda, also found by bin2chen and Critical
sfrxETH.beforeWithdraw first calls the beforeWithdraw of xERC4626, which decrements storedTotalAssets by the given amount. If the timestamp is greater than the rewardsCycleEnd, syncRewards is called. However, the problem is that the assets have not been transferred out yet, meaning asset.balanceOf(address(this)) still has the old value. On the other hand, storedTotalAssets was already updated. Therefore, the following calculation will be inflated by the amount for which the withdrawal was requested:
uint256 nextRewards = asset.balanceOf(address(this)) - storedTotalAssets_ - lastRewardAmount_;
This has severe consequences:
- During the following reward period,
lastRewardAmountis too high, which means that too many rewards are paid out to users who want to withdraw. A user could exploit this to steal the assets of other users. - When
syncRewards()is called the next time, it is possible that thenextRewardscalculation underflows becauselastRewardAmount > asset.balanceOf(address(this)). This is very bad becausesyncRewards()will be called in every withdrawal (after therewardsCycleEnd) and none of them will succeed because of the underflow. Depositing more also does not help here, it just increasesasset.balanceOf(address(this))andstoredTotalAssetsby the same amount, which does not eliminate the underflow.
Note that this bug does not require a malicious user or a targeted attack to surface. It can (and probably will) happen in practice just by normal user interactions with the vault (which is for instance shown in the PoC).
Proof Of Concept
Consider the following test:
function testTotalAssetsAfterWithdraw() public {
uint128 deposit = 1 ether;
uint128 withdraw = 1 ether;
// Mint frxETH to this testing contract from nothing, for testing
mintTo(address(this), deposit);
// Generate some sfrxETH to this testing contract using frxETH
frxETHtoken.approve(address(sfrxETHtoken), deposit);
sfrxETHtoken.deposit(deposit, address(this));
require(sfrxETHtoken.totalAssets() == deposit);
vm.warp(block.timestamp + 1000);
// Withdraw frxETH (from sfrxETH) to this testing contract
sfrxETHtoken.withdraw(withdraw, address(this), address(this));
vm.warp(block.timestamp + 1000);
sfrxETHtoken.syncRewards();
require(sfrxETHtoken.totalAssets() == deposit - withdraw);
}
This is a normal user interaction where a user deposits into the vault, and makes a withdrawal some time later. However, at this point the syncRewards() within the beforeWithdraw is executed. Because of that, the documented accounting mistake happens and the next call (in fact every call that will be done in the future) to syncRewards() reverts with an underflow.
Recommended Mitigation Steps
Call syncRewards() before decrementing storedTotalAssets, i.e.:
function beforeWithdraw(uint256 assets, uint256 shares) internal override {
if (block.timestamp >= rewardsCycleEnd) { syncRewards(); }
super.beforeWithdraw(assets, shares); // call xERC4626's beforeWithdraw AFTER
}
Then, asset.balanceOf(address(this)) and storedTotalAssets are still in sync within syncRewards().
FortisFortuna (Frax) commented:
Does this only occur if all users try to withdraw at the exact same time? If so, this is a known bug by us and the risk would be low in a real-life deployment scenario. We can also let the users know about the ramping of the rewards.
FortisFortuna (Frax) marked as duplicate
I do not think that this is a duplicate of #311. #311 (and the other issues that are linked there) describe a recoverable issue where the withdrawal for the last user fails (which was listed as a known issue of xERC4626) until the cycle ends.
The issue here that is described here and demonstrated in the PoC is a non-recoverable sfrxETH-specific issue (because sfrxETH potentially calls
syncRewards()in thebeforeWithdrawfunction) where withdrawals even fail after the cycle has ended. It also does not require all users to withdraw at the same time.
FortisFortuna (Frax) commented:
@Lambda What about 24 ?
@FortisFortuna Good catch did not see that, yes 24 addresses the same issue
FortisFortuna (Frax) confirmed and commented:
@Lambda I will mark yours as primary because it is better documented.
Here’s the proposed fix, which instead moves the syncRewards call to a modifier, so that it actually occurs before the withdraw instead of in beforeWithdraw. It also adds it to the other 4626 withdraw/redeem functions. Would appreciate feedback if you have any
https://github.com/FraxFinance/frxETH-public/pull/2/commits/1ec457c7f5faed618971fb29b9bcc6d54453b093
The modifier is currently missing for
mintandredeem, is that on purpose? Otherwise, it looks good to me
Whoops— nice catch, added here https://github.com/FraxFinance/frxETH-public/commit/996d528b46d1b2a0ac2e5b8f6d2138ccab8e03f5
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 order of operations in beforeWithdraw: storedTotalAssets is decremented before syncRewards is called, so syncRewards sees an asset balance that is higher than storedTotalAssets.
Impact
The inflated lastRewardAmount could let a user steal other users' assets, and subsequent syncRewards calls may underflow, causing all withdrawals after the rewards cycle end to fail.
What the attacker needed
Normal user interaction with the vault; time passes beyond the rewards cycle end
The fix
The fix was to move the syncRewards call to a modifier that runs before the withdrawal logic, and to add it to all 4626 withdraw and redeem functions.
How it was found
- Deposit frxETH into the sfrxETH vault.
- Advance time past the rewards cycle end.
- Withdraw some frxETH from the vault, which triggers syncRewards inside beforeWithdraw.
- Advance time again and call syncRewards, which reverts due to underflow.
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 vault is a smart contract that holds users' deposits and lets them withdraw later. When someone withdraws, the vault first updates its internal record of how much it holds, then sends the tokens. Here, a reward calculation ran between those two steps, so it saw the old token balance but the new internal record, producing a wrong reward amount. That wrong amount could later make the vault's maths fail, blocking all withdrawals.
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.