Skip to content
Business logic Audit finding Intermediate

Lack of Slippage Protection in withdraw / redeem Functions of the Vault

The withdraw and redeem functions of the PrizeVault lack slippage protection, so if the underlying yield vault suffers a loss while a user's transaction is pending, the user receives fewer assets than expected. An attacker could exploit this by causing a loss or front-running to make the user accept a worse exchange rate.

Aymen0909 Follow with cheatc0d3, turvy_fuzz and 7 others

Originally disclosed on Code4rena

Submitted by Aymen0909, also found by cheatc0d3, turvy_fuzz, trachev, FastChecker, Tripathi, Abdessamed, 0xmystery, btk, and Giorgio

https://github.com/code-423n4/2024-03-pooltogether/blob/main/pt-v5-vault/src/PrizeVault.sol#L454-L472 https://github.com/code-423n4/2024-03-pooltogether/blob/main/pt-v5-vault/src/PrizeVault.sol#L355-L366 https://github.com/code-423n4/2024-03-pooltogether/blob/main/pt-v5-vault/src/PrizeVault.sol#L489-L508

Issue Description

When a user who has deposited assets into the PrizeVault wishes to withdraw (or redeem) them, they can do so by calling either the withdraw or redeem functions.

Under normal conditions in the vault, users expect to receive their full deposited asset amount back, as there is a 1:1 exchange ratio between the asset amount and shares.

However, if the underlying yield vault experiences a loss, this exchange rate will decrease. This is highlighted in the previewWithdraw function (or previewRedeem/convertToAssets function):

function previewWithdraw(uint256 _assets) public view returns (uint256) {
    uint256 _totalAssets = totalAssets();

    // No withdrawals can occur if the vault controls no assets.
    if (_totalAssets == 0) revert ZeroTotalAssets();

    uint256 totalDebt_ = totalDebt();
    if (_totalAssets >= totalDebt_) {
        return _assets;
    } else {
        // Follows the inverse conversion of `convertToAssets`
        return _assets.mulDiv(totalDebt_, _totalAssets, Math.Rounding.Up);
    }
}

function convertToAssets(uint256 _shares) public view returns (uint256) {
    uint256 totalDebt_ = totalDebt();
    uint256 _totalAssets = totalAssets();
    if (_totalAssets >= totalDebt_) {
        return _shares;
    } else {
        // If the vault controls fewer assets than what has been deposited, a share will be worth a
        // proportional amount of the total assets. This can happen due to fees, slippage, or loss
        // of funds in the underlying yield vault.
        return _shares.mulDiv(_totalAssets, totalDebt_, Math.Rounding.Down);
    }
}
function totalAssets() public view returns (uint256) {
    return yieldVault.convertToAssets(yieldVault.balanceOf(address(this))) + _asset.balanceOf(address(this));
}

As shown, if the underlying yield vault experiences a loss, the total assets of the vault given by totalAssets() will decrease and might go below the total shares value totalDebt().

This will trigger the second if statement block, in which the calculated asset (or shares) amount will be converted using the ratio totalAssets/totalDebt (or its inverse for shares).

When a user redeems (or withdraws), if the yield vault experiences a loss while their transaction is pending (waiting in the mempool) and the total assets drop below the total vault debt, they will receive fewer assets after calling either the withdraw/redeem functions than they expected. Instead of a 1:1 ratio, they will use the current totalAssets/totalDebt ratio.

To illustrate this issue, consider the following scenario:

  • In the vault, we have totalAssets() = 51000 and totalDebt() = 50000.
  • Bob previously deposited 1000 tokens and received 1000 shares in return.
  • Bob wants to redeem 500 shares and expects to get his 500 tokens back since currently totalAssets() > totalDebt() (so there’s a 1:1 ratio).
  • While Bob’s transaction is pending in the mempool, the yield vault experiences a loss, and totalAssets() = 46000 drops below totalDebt() = 50000.
  • When Bob’s transaction goes through, he will receive: (500 * 46000) / 50000 = 460 < 500.
  • Thus, instead of receiving 500 tokens, he only gets 460 back, resulting in a loss of 40 tokens.

If Bob had known that the yield vault had experienced a loss, he would have waited until the exchange rate increased again to withdraw his full amount.

This issue is also present in the withdraw function, but in that case, the user will be burning more shares to get the same amount and still incurring a loss.

Impact

Both withdraw/redeem functions lack slippage protection, which can lead to users losing funds in the event of a yield vault loss.

Tools Used

VS Code

Recommended Mitigation

Both withdraw/redeem functions should include slippage protection parameters provided by the users (either minimum amount out for redeem function or maximum shares in for withdraw function).

Assessed type

Context

trmid (PoolTogether) confirmed and commented:

Providing depositors with slippage protection is a nice improvement! The suggested mitigation would break the ERC4626 spec requirements on the PrizeVault, so an alternate strategy will likely be used that either adds additional deposit and withdraw functions that have slippage params, or provides an external router that can provide this protection for the depositor.

d3e4 (warden) commented:

Is this not a design suggestion rather than a bug, and should belong in Analysis? To remain EIP-4626 compliant a slippage protection would have to be implemented in entirely new functionality; not by amending already existing code. Therefore, it cannot be said that there is something wrong with the current code.

btk (warden) commented:

@d3e4 - EIP-4626 states that:

If implementors intend to support EOA account access directly, they should consider adding an additional function call for deposit/mint/withdraw/redeem with the means to accommodate slippage loss or unexpected deposit/withdrawal limits, since they have no other means to revert the transaction if the exact output amount is not achieved.

hansfriese (judge) commented:

I think it’s eligible to be a Medium for the specific withdraw/redeem logic of PrizeVault. Will maintain it as a Medium.

trmid (PoolTogether) commented:

Mitigation here.


The original report as published on Code4rena, by Aymen0909. 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 withdraw and redeem functions do not allow users to specify a minimum acceptable output or maximum acceptable input, so they execute at whatever exchange rate exists when the transaction is processed.

What the attacker needed

User has deposited into the vault; yield vault experiences a loss; user's transaction is pending

The fix

The project added slippage protection parameters to the withdraw and redeem functions, as shown in the linked mitigation pull request.

How it was found

  1. Bob deposits 1000 tokens and receives 1000 shares.
  2. Bob calls redeem for 500 shares, expecting 500 tokens because totalAssets > totalDebt.
  3. While the transaction is pending, the yield vault loses value and totalAssets drops below totalDebt.
  4. The transaction executes and Bob receives only 460 tokens instead of 500.

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 put money into a vault, you get shares that represent your deposit. Normally you can take out the same amount you put in, but if the vault's investments lose value, each share is worth less. This report says the vault doesn't let you say 'don't give me less than X', so if the value drops while your withdrawal is waiting, you get less than you expected.

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 Aymen0909

View all

22 more disclosed reports, best first.

More from PoolTogether

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.