Skip to content
Business logic Audit finding Intermediate

Rewards will be locked if user transfer directly to pool without using deposit function

The farming contract calculates the total LP tokens in a pool by checking its own token balance, which includes tokens sent directly without using the deposit function. This causes rewards to be under-distributed and a portion locked forever when such direct transfers occur.

TrungOre Follow

Originally disclosed on Code4rena

Submitted by TrungOre

LPFarming.sol#L190

Reward will be locked in the farming, when user execute a direct transfer with lpToken to farm without using deposit.

Proof of Concept

“pls add this test to LpFarming.ts to check”

it("a part of rewards can't be distributed if user execute a direct transfer to farm", async() => {
      // manual mine new block
      await network.provider.send("evm_setAutomine", [false]);

      // prepare
      const attacker = bob;
      await lpTokens[0].transfer(alice.address, units(1000));
      await lpTokens[0].transfer(attacker.address, units(1000));
      await lpTokens[0].connect(alice).approve(farming.address, units(1000));
      await mineBlocks(1);

      // attacker direct deposit lp token to the pool
      await lpTokens[0].connect(attacker).transfer(farming.address, units(100));

      // create new pool
      await farming.add(10, lpTokens[0].address);
      await mineBlocks(1);
      expect(await farming.poolLength()).to.equal(1);

      let pool = await farming.poolInfo(0);
      expect(pool.lpToken).to.equal(lpTokens[0].address);
      expect(pool.allocPoint).to.equal(10);

      // create new epoch ==> balance of pool will be 1000
      let blockNumber = await ethers.provider.getBlockNumber();
      await farming.newEpoch(blockNumber + 1, blockNumber + 11, 100);

      // alice deposit
      await farming.connect(alice).deposit(0, units(100));
      await mineBlocks(1);

      expect(await jpeg.balanceOf(farming.address)).to.equal(1000);

      // when pool end, alice can just take 500 jpeg, and 500 jpeg will be locked in the contract forever !!!
      await mineBlocks(13);
      console.log("reward of alice: ", (await   farming.pendingReward(0, alice.address)).toString());
      expect(await farming.pendingReward(0, alice.address)).to.equal(BigNumber.from('500'));
    });

In the test above, the attacker transfers 100 lpToken to the farm without using deposit function, and alice deposit 100 lpToken. Because the contract uses pool.lpToken.balanceOf(address(this)) to get the total supply of lpToken in the pool, it will sum up 100 lpToken of attacker and 100 lpToken of alice. This will lead to the situation where Alice will only be able to claim 500 token (at epoch.endBlock), the rest will be locked in the pool forever. Not only with this pool, it also affects the following, a part of the reward will be locked in the pool when the farm end.

Tools Used

typescript

Recommended Mitigation Steps

Declare a new variable totalLPSupply to the struct PoolInfo, and use it instead of pool.lpToken.balanceOf(address(this)).

spaghettieth (JPEG’d) confirmed, but disagreed with High severity and commented:

Very minor issue, severity should be 0.

spaghettieth (JPEG’d) resolved and commented:

Fixed in jpegd/core#2.

LSDan (judge) decreased severity to Medium and commented:

I’m going to downgrade this to a medium. There is a possibility for lost funds given real world external factors (user stupidity).


The original report as published on Code4rena, by TrungOre. 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 uses its own token balance to determine total deposits, which can be inflated by direct transfers, instead of tracking deposits internally.

Impact

A portion of rewards may be permanently locked in the contract, and users receive less than expected.

What the attacker needed

User sends LP tokens directly to contract

The fix

The contract should track total LP supply with a dedicated variable instead of using the token balance.

How it was found

  1. Attacker transfers LP tokens directly to the farming contract without calling deposit.
  2. A new pool is created and a new epoch starts.
  3. Alice deposits LP tokens into the pool.
  4. When the epoch ends, Alice can only claim part of the reward, and the rest remains locked.

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.

A farming contract gives out rewards based on how many tokens each person deposits. It mistakenly counts tokens that were sent directly to the contract, not through the proper deposit function, as part of the total. This makes the reward calculation wrong, so some rewards get stuck and cannot be claimed.

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 TrungOre

View all

7 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.