Skip to content
Business logic Audit finding Intermediate

Vault.maxWithdraw returns asset amount that is too big for Vault.withdraw

The Vault's maxWithdraw function returns the maximum assets that can be withdrawn according to the adapter, but it does not account for the withdrawal fee that withdraw adds by increasing the required shares. An upstream contract that uses maxWithdraw's return value as input to withdraw will always revert because the fee pushes the required shares above the user's balance.

koxuan Follow

Originally disclosed on Code4rena

Submitted by koxuan

https://github.com/code-423n4/2023-01-popcorn/blob/main/src/vault/Vault.sol#L409-L411

https://github.com/code-423n4/2023-01-popcorn/blob/main/src/vault/Vault.sol#L211-L244

https://github.com/code-423n4/2023-01-popcorn/blob/main/src/vault/Vault.sol#L398-L416

Impact

ERC4626 standard requires maxWithdraw to return the maximum amount of underlying assets that can be withdrawn from the owner balance with a single withdraw call. withdraw in Vault implements a fee which is not calculated in maxWithdraw in Vault. Therefore, upstream contracts that call maxWithdraw and use the return value for withdraw will always revert.

Proof of Concept

Upstream contract calls maxWithdraw to determine maximum amount of assets user can withdraw. adapter is the wrapper that allows interaction with the underlying ERC4626 protocol.

    function maxWithdraw(address caller) external view returns (uint256) {
        return adapter.maxWithdraw(caller);
    }

The upstream contract uses the return amount of assets as the input for withdraw. Notice that there is a withdrawal fee added by increasing the shares required. Since maxWithdraw would have returned the maximum assets that can be returned, increasing the shares required from withdrawal fee will mean that the shares required will exceed shares that user have. This will cause a revert during the transfer of shares from user to vault.

    function withdraw(
        uint256 assets,
        address receiver,
        address owner
    ) public nonReentrant syncFeeCheckpoint returns (uint256 shares) {
        if (receiver == address(0)) revert InvalidReceiver();

        shares = convertToShares(assets);

        uint256 withdrawalFee = uint256(fees.withdrawal);

        uint256 feeShares = shares.mulDiv(
            withdrawalFee,
            1e18 - withdrawalFee,
            Math.Rounding.Down
        );

        shares += feeShares;

        if (msg.sender != owner)
            _approve(owner, msg.sender, allowance(owner, msg.sender) - shares);

        _burn(owner, shares);

        if (feeShares > 0) _mint(feeRecipient, feeShares);

        adapter.withdraw(assets, receiver, address(this));

        emit Withdraw(msg.sender, receiver, owner, assets, shares);
    }

    function redeem(uint256 shares) external returns (uint256) {
        return redeem(shares, msg.sender, msg.sender);
    }

Note, this applies to all max* functions too in Vault. They all delegate to adapter which does not include withdrawal or deposit fee.

    /// @return Maximum amount of underlying `asset` token that may be deposited for a given address. Delegates to adapter.
    function maxDeposit(address caller) public view returns (uint256) {
        return adapter.maxDeposit(caller);
    }

    /// @return Maximum amount of vault shares that may be minted to given address. Delegates to adapter.
    function maxMint(address caller) external view returns (uint256) {
        return adapter.maxMint(caller);
    }

    /// @return Maximum amount of underlying `asset` token that can be withdrawn by `caller` address. Delegates to adapter.
    function maxWithdraw(address caller) external view returns (uint256) {
        return adapter.maxWithdraw(caller);
    }

    /// @return Maximum amount of shares that may be redeemed by `caller` address. Delegates to adapter.
    function maxRedeem(address caller) external view returns (uint256) {
        return adapter.maxRedeem(caller);
    }

Recommended Mitigation Steps

Recommend calculating the withdrawal fee and deducting it in maxWithdraw. Same for the withdrawal and deposit fees for all the max* functions.

RedVeil (Popcorn) acknowledged


The original report as published on Code4rena, by koxuan. 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 maxWithdraw function delegates directly to the adapter without including the vault's withdrawal fee, so the returned maximum is too high for the actual withdraw logic.

Impact

Upstream contracts that rely on maxWithdraw to determine a withdrawable amount will always revert when they call withdraw with that amount.

What the attacker needed

Upstream contract uses maxWithdraw result; vault has a non-zero withdrawal fee

The fix

The withdrawal fee should be calculated and deducted in maxWithdraw, and the same should be done for deposit and withdrawal fees in all max* functions.

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 lets people deposit assets and later withdraw them. The vault has a helper function that tells other programs the largest amount a user can withdraw, but it forgets that the vault also charges a fee when you actually withdraw. So another program trusts that number, tries to withdraw exactly that much, and the transaction fails because the fee makes the cost slightly higher than what the user has.

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 koxuan

View all

7 more disclosed reports, best first.

More from Popcorn contest

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.