previewWithdraw calculates shares wrongly
The previewWithdraw function incorrectly subtracts the fee when calculating shares, returning fewer shares than needed. This causes the withdraw function to always revert, preventing users from withdrawing their requested assets through that function.
Submitted by kenzo, also found by PP1004, pedroais, and CertoraInc
The fee is wrongly accounted for in previewWithdraw.
Impact
Function returns wrong result;
Additionally, withdraw(assets,to,from) will always revert. (The user can still withdraw his assets via other functions).
Proof of Concept
The previewWithdraw function returns less shares than the required assets (notice the substraction):
uint256 amountWithdrawn;
uint256 _fee = assets.mul(feeBPS).div(10000);
amountWithdrawn = assets.sub(_fee);
shares = convertToShares(amountWithdrawn);
This won’t work, because if the user wants to receive amount of assets, he needs to burn more shares than that to account for the fee. Not less.
This will also make withdraw(assets,to,from) revert, because it takes the amount of shares from previewWithdraw, and then checks how much assets were really sent to the user, and verifies that it’s at least how much he asked for:
uint256 expectedShares = previewWithdraw(assets);
uint256 assetsReceived = _withdraw(expectedShares, receiver);
require(assetsReceived >= assets, "You cannot withdraw the amount of assets you expected");
But since the expectedShares is smaller than the original amount, and since _withdraw deducts the fee from expectedShares, then always assets > assetsReceived, and the function will revert.
Recommended Mitigation Steps
The amount of shares that previewWithdraw should return is: convertToShares(assets.add(assets.mul(feeBPS).div((10000.sub(feeBPS)))) I prove this mathematically in this image.
Keeping it as medium severity because while protocol functionality is impacted, users can withdraw through the
redeem()function.
The new solution, thank you, Kenzo.
/// @notice * EIP 4626 * function previewWithdraw(uint256 assets) public view returns (uint256 shares) { if (totalSupply == 0) { shares = 0; } else { shares = convertToShares( assets.add(assets.mul(feeBPS).div((uint(10000).sub(feeBPS)))) ); } }
Keeping it as medium severity because while protocol functionality is impacted, users can withdraw through the
redeem()function.Note that we use the
withdrawthat relies on msg.sender as the caller in production so were not affected in practice. It’s interesting that various wardens have different answers to the solution for this issue. This one seems best and I’m going with it for now!
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 fee is subtracted from the assets when calculating shares, but it should be added to account for the fee that will be deducted later.
Impact
The withdraw function always reverts, but users can still withdraw via other functions such as redeem.
What the attacker needed
None
The fix
The previewWithdraw function should calculate shares by adding the fee to the assets before converting to shares, as shown in the recommended formula.
How it was found
- Call previewWithdraw with a desired asset amount.
- Observe it returns fewer shares than needed.
- Call withdraw with that asset amount.
- The function reverts because assetsReceived is less than assets.
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 bug in a function that calculates how many shares a user must burn to receive a certain amount of assets. The calculation wrongly subtracts the fee instead of adding it, so the withdraw function fails because it expects more assets than it receives.
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.