YearnV2YieldSource wrong subtraction in withdraw
The withdraw function in a Yearn V2 yield source contract subtracts the current token balance from the previous balance, which is backwards. This causes an integer underflow that makes all withdrawals fail, locking users' funds.
Submitted by cmichel, also found by 0xRajeev
When withdrawing from the vault, one redeems yTokens for tokens, thus the token balance of the contract should increase after withdrawal. But the contract subtracts the currentBalance from the previousBalance:
uint256 yShares = _tokenToYShares(amount);
uint256 previousBalance = token.balanceOf(address(this));
// we accept losses to avoid being locked in the Vault (if losses happened for some reason)
if(maxLosses != 0) {
vault.withdraw(yShares, address(this), maxLosses);
} else {
vault.withdraw(yShares);
}
uint256 currentBalance = token.balanceOf(address(this));
// @audit-issue this seems wrong
return previousBalance.sub(currentBalance);
All vault withdrawals fail due to the integer underflow as the previousBalance is less than currentBalance. Users won’t be able to get back their investment.
Recommend that It should return currentBalance > previousBalance ? currentBalance - previousBalance : 0
kamescg (PoolTogether) confirmed and patched:
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 subtraction order is reversed: it subtracts the new balance from the old balance instead of the other way around, causing an underflow.
Impact
All withdrawals fail, so users cannot retrieve their investments.
What the attacker needed
None
The fix
The function should return the positive difference only if the current balance is greater than the previous balance, otherwise zero.
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, the balance should go up. But this code checks the balance before and after and subtracts the new balance from the old one, which gives a negative number. Computers can't handle negative numbers in this context, so the withdrawal fails and you can't get your money 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.