Skip to content
Business logic Audit finding Intermediate

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.

hickuphh3 Follow

Originally disclosed on Code4rena

Submitted by hickuphh3

yVaultLPFarming.sol#L169-L170

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, then withdrawJPEG() 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
  • jpeg could be accidentally included in the StrategyConfig, resulting in JPEG being converted to other assets
  • A future implementation takes a fee on the jpeg to 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.

LSDan (judge) commented:

I disagree with the sponsor. This is high risk.


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

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

  1. Set up the vault and farming contract with initial deposits.
  2. Change the farming pool in the yVault to a new address.
  3. Call withdrawJPEG() to move funds to the new farm.
  4. 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.

More by hickuphh3

View all

43 more disclosed reports, best first.

More from JPEG'd contest

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.