Skip to content
Business logic Audit finding Intermediate

YearnTokenAdapter allows a maximum loss of 100% when withdrawing

The YearnTokenAdapter contract hardcodes a maximum loss of 100% when withdrawing from a Yearn vault, meaning there is no slippage check. An attacker could exploit this by manipulating the vault's share price to cause a loss of funds for users withdrawing through the adapter.

Ruhum Follow

Originally disclosed on Code4rena

Submitted by Ruhum

YearnTokenAdapter.sol#L13 YearnTokenAdapter.sol#L43

YearnTokenAdapter allows slippage of 100% when withdrawing from the vault which will cause a loss of funds.

Here’s the documentation straight from the vault contract: https://github.com/yearn/yearn-vaults/blob/main/contracts/Vault.vy#L1025-L1073 It allows the user to specify the maxLoss as the last parameter. It determines how many shares can be burned to fulfill the withdrawal. Currently, the contract uses 10.000 which is 100%. Meaning there is no slippage check at all. This is bound to cause a loss of funds.

I’d suggest letting the user determine the slippage check themselves instead of hardcoding it.

Proof of Concept

Current maxLoss value: https://github.com/code-423n4/2022-05-alchemix/blob/main/contracts-full/adapters/yearn/YearnTokenAdapter.sol#L13

call to Yearn vault’s withdraw() function: https://github.com/code-423n4/2022-05-alchemix/blob/main/contracts-full/adapters/yearn/YearnTokenAdapter.sol#L43

Recommended Mitigation Steps

Allow the user to specify the slippage value themselves:

    function unwrap(uint256 amount, address recipient, uint maxLoss) external override returns (uint256) {
        TokenUtils.safeTransferFrom(token, msg.sender, address(this), amount);

        uint256 balanceBefore = TokenUtils.safeBalanceOf(token, address(this));

        uint256 amountWithdrawn = IYearnVaultV2(token).withdraw(amount, recipient, maxLoss);

        uint256 balanceAfter = TokenUtils.safeBalanceOf(token, address(this));

        // If the Yearn vault did not burn all of the shares then revert. This is critical in mathematical operations
        // performed by the system because the system always expects that all of the tokens were unwrapped. In Yearn,
        // this sometimes does not happen in cases where strategies cannot withdraw all of the requested tokens (an
        // example strategy where this can occur is with Compound and AAVE where funds may not be accessible because
        // they were lent out).
        if (balanceBefore - balanceAfter != amount) {
            revert IllegalState();
        }

        return amountWithdrawn;
    }

If you don’t want to change the ITokenAdapter interface you can also leave the value blank. The vault will then use the default value (0.01%)

0xfoobar (Alchemix) acknowledged, disagreed with severity and commented:

This could be made more configurable by the end user but yearn vaults do not frequently experience high slippage. Slippage is handled upstream in the Alchemist contract. The reason why this slippage is set to 100% is so to permit handling of slippage in the Alchemist for all cases.

0xleastwood (judge) decreased severity to Medium and commented:

Because we can’t know how the yearn strategy implements withdrawals, its possible that it might contain custom swap logic which exposes itself to sandwich attacks. However, at face value, the current use of MAXIMUM_SLIPPAGE allows the contract to successfully unwrap their tokens under poor network conditions, but it makes sense for the user to have more control over this. Downgrading this to medium risk as I believe it is more in line with that.


The original report as published on Code4rena, by Ruhum. 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

The contract hardcodes the maximum loss parameter to 100% (10,000 basis points) when calling the Yearn vault's withdraw function, effectively disabling any slippage protection.

Impact

Users could lose a significant portion of their funds when withdrawing due to unfavourable exchange rates or sandwich attacks, as the contract does not enforce a minimum received amount.

What the attacker needed

User interacts with the adapter; Yearn vault experiences slippage

The fix

The contract should allow the user to specify the maximum acceptable loss (slippage) instead of hardcoding it, or leave the parameter blank to use the vault's default of 0.01%.

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.

When you withdraw money from a savings account, you expect to get back roughly what you put in. Here, the code allows the withdrawal to lose up to 100% of the value without stopping, so you could get much less than expected. An attacker might manipulate the system to make that loss happen.

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 Ruhum

View all

55 more disclosed reports, best first.

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