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:
This has severe consequences:
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).
Consider the following test:
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.
Call syncRewards() before decrementing storedTotalAssets, i.e.:
Then, asset.balanceOf(address(this)) and storedTotalAssets are still in sync within syncRewards().
FortisFortuna (Frax) commented:
 FortisFortuna (Frax) marked as duplicate
Lambda (warden) commented:
FortisFortuna (Frax) commented:
Lambda (warden) commented:
FortisFortuna (Frax) confirmed and commented:
corddry (Frax) commented:
Lambda (warden) commented:
corddry (Frax) commented:
