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
strategiesarray, 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...
- Removes the LP tokens from the ERC1155 holding them for collateral.
doTakeCollateral(strategies[strategyId].vault, lpTakeAmt);
- Calculates the number of LP tokens to withdraw from the vault.
uint256 amtLPToRemove = vault.balanceOf(address(this)) - amountLpWithdraw;
vault.withdraw(amtLPToRemove, address(this));
-
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).
-
Withdraw the underlying token from Compound.
doWithdraw(collToken, amountShareWithdraw);
- Pay back the borrowed token to Compound.
doRepay(borrowToken, amountRepay);
- Validate that this situation does not put us above the maxLTV for our loans.
_validateMaxLTV(strategyId);
- Sends the remaining borrow token that weren't paid back and withdrawn underlying tokens to the user.
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
Tool used
Manual Review
Recommendation
Add an additional line to the withdrawInternal() function to refund all LP tokens as well:
doRefund(borrowToken);
doRefund(collToken);
+ doRefund(address(vault));
Discussion
Gornutz
duplicate of 34
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
- Call closePosition or closePositionFarm, which both call withdrawInternal.
- The contract removes LP tokens from the ERC1155 collateral.
- It calculates the LP tokens to remove from the vault, holding back amountLpWithdraw.
- It withdraws the underlying token from Compound and repays the borrowed token.
- It validates the maxLTV and refunds the borrow and collateral tokens to the user.
- 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.