Skip to content
Business logic Audit finding Intermediate

LP tokens are not sent back to withdrawing user

The withdraw function in IchiVaultSpell.sol unwinds a user's position and refunds the underlying tokens but never sends back the LP tokens the user asked to keep. As a result, those LP tokens remain permanently stuck in the contract.

rvierdiiev Follow with minhtrng, Dug and 11 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-blueberry-judging/issues/151

Found by

rvierdiiev, minhtrng, Dug, Jeiwan, obront, chaduke, koxuan, sinarette, Ch_301, cergyk, evan, berndartmueller, 0x52, Bauer

Summary

When users withdraw their assets from IchiVaultSpell.sol, the function unwinds their position and sends them back their assets, but it never sends them back the amount they requested to withdraw, leaving the tokens stuck in the Spell contract.

Vulnerability Detail

When a user withdraws from IchiVaultSpell.sol, they either call closePosition() or closePositionFarm(), both of which make an internal call to withdrawInternal().

The following arguments are passed to the function:

  • strategyId: an index into the strategies array, which specifies the Ichi vault in question
  • collToken: the underlying token, which is withdrawn from Compound
  • amountShareWithdraw: the number of underlying tokens to withdraw from Compound
  • borrowToken: the token that was borrowed from Compound to create the position, one of the underlying tokens of the vault
  • amountRepay: the amount of the borrow token to repay to Compound
  • amountLpWithdraw: the amount of the LP token to withdraw, rather than trade back into borrow tokens

In order to accomplish these goals, the contract does the following...

  1. Removes the LP tokens from the ERC1155 holding them for collateral.
Solidity
doTakeCollateral(strategies[strategyId].vault, lpTakeAmt);
  1. Calculates the number of LP tokens to withdraw from the vault.
Solidity
uint256 amtLPToRemove = vault.balanceOf(address(this)) - amountLpWithdraw;
vault.withdraw(amtLPToRemove, address(this));
  1. Converts the non-borrowed token that was withdrawn in the borrowed token (not copying the code in, as it's not relevant to this issue).

  2. Withdraw the underlying token from Compound.

Solidity
doWithdraw(collToken, amountShareWithdraw);
  1. Pay back the borrowed token to Compound.
Solidity
doRepay(borrowToken, amountRepay);
  1. Validate that this situation does not put us above the maxLTV for our loans.
Solidity
_validateMaxLTV(strategyId);
  1. Sends the remaining borrow token that weren't paid back and withdrawn underlying tokens to the user.
Solidity
doRefund(borrowToken);
doRefund(collToken);

Crucially, the step of sending the remaining LP tokens to the user is skipped, even though the function specifically does the calculations to ensure that amountLpWithdraw is held back from being taken out of the vault.

Impact

Users who close their positions and choose to keep LP tokens (rather than unwinding the position for the constituent tokens) will have their LP tokens stuck permanently in the IchiVaultSpell contract.

Code Snippet

https://github.com/sherlock-audit/2023-02-blueberry/blob/main/contracts/spell/IchiVaultSpell.sol#L276-L330

Tool used

Manual Review

Recommendation

Add an additional line to the withdrawInternal() function to refund all LP tokens as well:

Diff
  doRefund(borrowToken);
  doRefund(collToken);
+ doRefund(address(vault));

Discussion

Gornutz

duplicate of 34

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

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 withdrawInternal function calculates how many LP tokens to hold back for the user but omits the refund call that would transfer them to the user.

Impact

Users who close their positions and choose to keep LP tokens will have those tokens stuck permanently in the contract.

What the attacker needed

A user with an open position in IchiVaultSpell; user chooses to keep LP tokens on withdrawal

The fix

The withdrawInternal function should be fixed to also refund the LP tokens to the user.

How it was found

  1. Call closePosition or closePositionFarm, which both call withdrawInternal.
  2. The contract removes LP tokens from the ERC1155 collateral.
  3. It calculates the LP tokens to remove from the vault, holding back amountLpWithdraw.
  4. It withdraws the underlying token from Compound and repays the borrowed token.
  5. It validates the maxLTV and refunds the borrow and collateral tokens to the user.
  6. The remaining LP tokens are not refunded and stay in the contract.

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 a receipt token called an LP token. This report says that when you close your position and ask to keep some of those receipt tokens, the contract keeps them instead of giving them to you, so you cannot get them back.

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 rvierdiiev

View all

72 more disclosed reports, best first.

More from Blueberry Update #3

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.