Skip to content
Business logic Audit finding Intermediate

YaxisVaultAdapter.sol#withdraw() will most certainly fail

The withdraw function in YaxisVaultAdapter.sol converts the requested token amount to vault shares, but due to precision loss the actual tokens received are less than the requested amount, causing the subsequent safeTransfer to revert from insufficient balance. The report states this will fail 99.99% of the time unless the price per share equals 1e18.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

The actual token withdrawn from vault.withdraw() will most certainly less than the _amount, due to precision loss in _tokensToShares() and vault.withdraw().

As a result, IDetailedERC20(_token).safeTransfer(_recipient, _amount) will revert due to insufficant balance.

Based on the simulation we ran, it will fail 99.99% of the time unless the pps == 1e18.

https://github.com/code-423n4/2021-11-yaxis/blob/146febcb61ae7fe20b0920849c4f4bbe111c6ba7/contracts/v3/alchemix/adapters/YaxisVaultAdapter.sol#L68-L72

function withdraw(address _recipient, uint256 _amount) external override onlyAdmin {
    vault.withdraw(_tokensToShares(_amount));
    address _token = vault.getToken();
    IDetailedERC20(_token).safeTransfer(_recipient, _amount);
}

https://github.com/code-423n4/2021-11-yaxis/blob/146febcb61ae7fe20b0920849c4f4bbe111c6ba7/contracts/v3/Vault.sol#L181-L187

function withdraw(
    uint256 _shares
)
    public
    override
{
    uint256 _amount = (balance().mul(_shares)).div(IERC20(address(vaultToken)).totalSupply());

Recommendation

Change to:

function withdraw(address _recipient, uint256 _amount) external override onlyAdmin {
    address _token = vault.getToken();
    uint256 beforeBalance = IDetailedERC20(_token).balanceOf(address(this));

    vault.withdraw(_tokensToShares(_amount));

    IDetailedERC20(_token).safeTransfer(
        _recipient,
        IDetailedERC20(_token).balanceOf(address(this)) - beforeBalance
    );
}

Xuefeng-Zhu (yAxis) confirmed

The original report as published on Code4rena, by WatchPug. 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 precision loss in the share-to-token conversion causes the withdrawn token amount to be less than the requested amount, leading to a failed transfer.

Impact

The withdraw function will fail 99.99% of the time unless the price per share is exactly 1e18, preventing users from withdrawing their funds.

What the attacker needed

Admin calls withdraw; vault has sufficient liquidity

The fix

The recommendation is to calculate the actual amount received by checking the balance difference before and after the vault withdrawal, and transfer that amount instead of the requested amount.

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 where a function tries to send a specific amount of tokens but ends up with slightly less due to rounding errors when converting between shares and tokens. When it tries to send the full amount, the contract doesn't have enough tokens, so the transaction fails.

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 WatchPug

View all

197 more disclosed reports, best first.

More from yAxis 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.