Skip to content
Business logic Audit finding Intermediate

NFTXLPStaking Is Subject To A Flash Loan Attack That Can Steal Nearly All Rewards/Fees That Have Accrued For A Particular Vault

The LPStaking contract does not lock stakes or track staking duration, so an attacker can use a flash loan to stake a large amount, claim nearly all accrued rewards, and unstake in one transaction. This leaves legitimate stakers with very little reward.

NFTX contest

Originally disclosed on Code4rena

The LPStaking contract does not require that a stake be locked for any period of time. The LPStaking contract also does not track how long your stake has been locked. So an attacker Alice can stake, claim rewards, and unstake, all in one transaction. If Alice utilizes a flash loan, then she can claim nearly all of the rewards for herself, leaving very little left for the legitimate stakers.

The fact that the NFTXVaultUpgradeable contract contains a native flashLoan function makes this attack that much easier, although it would still be possible even without that due to flashloans on Uniswap, or wherever else the nftX token is found.

Since a flash loan will easily dwarf all of the legitimate stakers’ size of stake, the contract will erroneously award nearly all of the rewards to Alice.

  1. Wait until an NFTX vault has accrued any significant amount of fees/rewards
  2. FlashLoanBorrow a lot of ETH using any generic flash loan provider
  3. FlashLoanBorrow a lot of nftx-vault-token using NFTXVaultUpgradeable.flashLoan()
  4. Deposit the ETH and nftx-vault-token’s into Uniswap for Uniswap LP tokens by calling Uniswap.addLiquidity()
  5. Stake the Uniswap LP tokens in NFTXLPStaking by calling NFTXLPStaking.deposit()
  6. Claim nearly all of the rewards that have accrued for this vault due to how large the flashLoaned deposit is relative to all of the legitimate stakes by calling NFTXLPStaking.claimRewards()
  7. Remove LP tokens from NFTXLPStaking by calling NFTXLPStaking.exit();
  8. Withdraw ETH and nftx-vault-token’s by calling Uniswap.removeLiquidity();
  9. Pay back nftx-vault-token flash loan
  10. Pay back ETH flash loan

See GitHub issue page for an in-depth example.

Recommend requiring that staked LP tokens be staked for a particular period of time before they can be removed. Although a very short time frame (a few blocks) would avoid flash loan attacks, this attack could still be performed over the course of a few blocks less efficiently. Ideally, you would want the rewards to reflect the product of the amount staked and the duration that they’ve been staked, as well as having a minimum time staked.

Alternatively, if you really want to allow people to have the ability to remove their stake immediately, then only allow rewards to be claimed for stakes that have been staked for a certain period of time. Users would still be able to remove their LP tokens, but they could no longer siphon off rewards immediately.

0xKiwi (NFTX) disputed:

After looking at the code, this is not possible. The dividend token code takes into consideration the current unclaimed rewards and when a deposit is made that value is deducted.

cemozer (Judge) commented:

@0xKiwi do you mind showing where in code that occurs?

The original report as published on Code4rena. 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 staking contract does not enforce a minimum staking duration or weight rewards by staking time, so a large short-lived stake can dominate reward distribution.

Impact

An attacker could steal nearly all rewards and fees that have accrued for a vault, leaving very little for legitimate stakers; the project disputed that the attack is possible.

What the attacker needed

A vault with accrued rewards; access to a flash loan provider; ability to interact with the contracts

The fix

The staking contract should require a minimum staking period and weight rewards by both stake amount and staking duration, or only allow reward claims for stakes held for a certain time.

How it was found

  1. Wait until an NFTX vault has accrued a significant amount of fees or rewards.
  2. Borrow a large amount of ETH from a flash loan provider.
  3. Borrow a large amount of nftx-vault-token using the vault's flashLoan function.
  4. Add liquidity to Uniswap with the borrowed ETH and nftx-vault-token to get LP tokens.
  5. Stake the LP tokens in NFTXLPStaking.
  6. Claim the accrued rewards, then exit the stake, remove liquidity, and repay the flash loans.

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 flash loan lets someone borrow a huge amount of cryptocurrency for a single transaction, as long as they pay it back in the same transaction. Here, because the staking contract doesn't require you to keep your stake for any length of time, an attacker can borrow a lot, stake it briefly, grab most of the rewards, and return the loan, all before anyone else can react.

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