yVaultLPFarming : No guarantee JPEG currentBalance > previousBalance
The yVaultLPFarming contract assumes that the JPEG balance reported by the vault never decreases, but if it does, a subtraction underflow causes deposits and withdrawals to fail. An attacker could exploit this by triggering a farm migration or strategy change that reduces the balance, locking users' funds.
Submitted by hickuphh3
yVault users participating in the farm have to trust that:
vault.balanceOfJPEG()returns the correct claimable JPEG amount by its strategy / strategies- the strategy / strategies will send all claimable JPEG to the farm
Should either of these assumptions break, then it could possibly be the case that currentBalance is less than previousBalance, causing deposits and crucially, withdrawals to fail due to subtraction overflow.
Proof of Concept
For instance,
- Farm migration occurs. A new farm is set in
yVault, thenwithdrawJPEG()is called, which sends funds to the new farm. Users of the old farm would be unable to withdraw their deposits.
it.only("will revert old farms' deposits and withdrawals if yVault migrates farm", async () => {
// 0. setup
await token.mint(owner.address, units(1000));
await token.approve(yVault.address, units(1000));
await yVault.depositAll();
await yVault.approve(lpFarming.address, units(1000));
// send some JPEG to strategy prior to deposit
await jpeg.mint(strategy.address, units(100));
// deposit twice, so that the second deposit will invoke _update()
await lpFarming.deposit(units(250));
await lpFarming.deposit(units(250));
// 1. change farm and call withdrawJPEG()
await yVault.setFarmingPool(user1.address);
await yVault.withdrawJPEG();
// deposit and withdrawal will fail
await expect(lpFarming.deposit(units(500))).to.be.revertedWith('reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
await expect(lpFarming.withdraw(units(500))).to.be.revertedWith('reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
- Strategy migration occurs, but JPEG funds held by the old strategy were not claimed, causing
vault.balanceOfJPEG()to report a smaller amount than previously recorded jpegcould be accidentally included in the StrategyConfig, resulting in JPEG being converted to other assets- A future implementation takes a fee on the
jpegto be claimed
Recommended Mitigation Steps
A simple fix would be to return if currentBalance ≤ previousBalance. A full fix would properly handle potential shortfall.
if (currentBalance <= previousBalance) return;
spaghettieth (JPEG’d) confirmed, but disagreed with High severity and commented:
The issue can be reproduced, but due to the extremely specific cases in which this happens the severity should be lowered to 2.
spaghettieth (JPEG’d) resolved and commented:
Fixed in jpegd/core#7.
I disagree with the sponsor. This is high risk.
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 contract does not handle the case where the current JPEG balance is less than the previous balance, leading to an arithmetic underflow.
Impact
Deposits and withdrawals could fail, potentially locking user funds, but the sponsor noted the triggering conditions are extremely specific.
What the attacker needed
Farm migration or strategy change; JPEG balance decreases
The fix
The issue was fixed by returning early if the current balance is less than or equal to the previous balance.
How it was found
- Set up the vault and farming contract with initial deposits.
- Change the farming pool in the yVault to a new address.
- Call withdrawJPEG() to move funds to the new farm.
- Attempt to deposit or withdraw from the old farm, which reverts due to underflow.
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.
This is a smart contract bug where the farming contract expects the amount of JPEG tokens it can claim to always go up. If that amount goes down, a calculation underflows (like trying to subtract a bigger number from a smaller one), and the contract stops working for deposits and withdrawals. Users could get stuck unable to take their money out.
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.