Skip to content
Business logic Audit finding Intermediate

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.

kenzo Follow

Originally disclosed on Code4rena

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.

bghughes (Rubicon) confirmed

HickupHH3 (judge) commented:

Keeping it as medium severity because while protocol functionality is impacted, users can withdraw through the redeem() function.

bghughes (Rubicon) commented:

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)))) ); } }

bghughes (Rubicon) commented:

Keeping it as medium severity because while protocol functionality is impacted, users can withdraw through the redeem() function.

Note that we use the withdraw that 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!


The original report as published on Code4rena, by kenzo. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Call previewWithdraw with a desired asset amount.
  2. Observe it returns fewer shares than needed.
  3. Call withdraw with that asset amount.
  4. 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.

More by kenzo

View all

53 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.