YearnVault.sol#pull() will most certainly fail
The YearnVault.pull() function calculates the amount of yTokens to withdraw using integer division, which can truncate the result. This causes the actual tokens received to be slightly less than the requested amount, leading to a revert when the contract tries to transfer the full requested amount to the user.
Submitted by WatchPug
for (uint256 i = 0; i < _yTokens.length; i++) {
if (tokenAmounts[i] == 0) {
continue;
}
IYearnVault yToken = IYearnVault(_yTokens[i]);
uint256 yTokenAmount = ((tokenAmounts[i] * (10**yToken.decimals())) / yToken.pricePerShare());
uint256 balance = yToken.balanceOf(address(this));
if (yTokenAmount > balance) {
yTokenAmount = balance;
}
if (yTokenAmount == 0) {
continue;
}
yToken.withdraw(yTokenAmount, to, maxLoss);
(tokenAmounts[i], address(this));
}
actualTokenAmounts = tokenAmounts;
The actual token withdrew from yToken.withdraw() will most certainly be less than the tokenAmounts[i], due to precision loss in the calculation of yTokenAmount.
As a result, IERC20(_vaultTokens[i]).safeTransfer(to, actualTokenAmounts[i]); in LpIssuer.sol#withdraw() will revert due to insufficant balance.
Recommendation
Change to:
tokenAmounts[i] = yToken.withdraw(yTokenAmount, to, maxLoss);
MihanixA (Mellow Protocol) confirmed and disagreed with severity:
Actually I don’t see how this could lead to fund loss. I think this one is a bug. @0xleastwood what do you think?
0xleastwood (judge) commented:
my understanding is that users won’t be able to withdraw pushed funds @MihanixA
so fund loss is related to not being able to withdraw rather than by extracting value from the protocol
Oxn1ck (Mellow Protocol) commented:
While we agree that this will prevent full withdrawal of the funds, that wil be limited to only a couple of wei’s which is the yearn precision loss. So in case you put 100eth you will be able to recover 100eth - 1wei. So we’d rather name the issue “some small amounts cannot be withdrawn from the pool”
0xleastwood (judge) commented:
If my understanding is correct,
YearnVault._pullwill withdrawyTokenAmountrepresenting the yToken’s shares and then withdraw on this amount but returntokenAmountswhere the amount withdrawn is typically less than the amount intended to be withdrawn.LpIssuer.withdraw()will expectactualTokenAmountsto be available to be transferred which isn’t exactly in the contract’s balance.
- https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/YearnVault.sol#L90
- https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/LpIssuer.sol#L152
Let’s use an example:
- Alice calls
LpIssuer.withdraw()withtokensAmount[0]equal to 100 tokens. Let’s ignore thelpTokenAmountargument for the sake of this example._subvault().pullis called on thistokensAmount[0].yTokenAmountis calculated according to((tokenAmounts[i] * (10**yToken.decimals())) / yToken.pricePerShare());which potentially leads to a slightly truncated output.- This truncated output represents the shares belonging to the user which is then parsed to
yToken.withdraw().yToken.withdraw()is likely less than 100 tokens and is sent to theLpIssuer.solcontract butactualTokenAmounts[0]is equal to 100 tokens.LpIssuer.withdraw()attempts to send tokens to the withdrawer but is unable as the contract does not have sufficient balance.IERC20(_vaultTokens[i]).safeTransfer(to, actualTokenAmounts[i]);- If I’m not mistaken, it seems like this issue would be apparent on any withdrawal amount (assuming there is some amount truncated).
- There is also an important edge case where the amount to withdraw from the yearn vault is greater than the available contract balance, it will always revert.
Oxn1ck (Mellow Protocol) commented:
Agreed, thank you!
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 integer division truncation when converting token amounts to yToken shares, causing the withdrawn amount to be less than the recorded amount.
Impact
Users may be unable to withdraw their full funds, with the shortfall limited to a few wei due to precision loss, and in some cases withdrawals may always revert if the requested amount exceeds the available balance.
What the attacker needed
User has funds deposited in the vault; User initiates a withdrawal
The fix
The fix is to update tokenAmounts[i] with the actual amount returned by yToken.withdraw().
How it was found
- Alice calls LpIssuer.withdraw() with tokensAmount[0] equal to 100 tokens.
- _subvault().pull is called on this tokensAmount[0].
- yTokenAmount is calculated using integer division, leading to a truncated output.
- yToken.withdraw() is called with the truncated amount, returning less than 100 tokens.
- LpIssuer.withdraw() attempts to transfer actualTokenAmounts[0] (100 tokens) but the contract has insufficient balance, causing a revert.
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 from a vault, the code calculates how many shares to sell to get the tokens you asked for. Because of rounding down in the maths, you get slightly fewer tokens than requested. The contract then tries to send you the full amount but doesn't have enough, so the withdrawal 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.