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.
- Wait until an NFTX vault has accrued any significant amount of fees/rewards
FlashLoanBorrowa lot of ETH using any generic flash loan providerFlashLoanBorrowa lot of nftx-vault-token usingNFTXVaultUpgradeable.flashLoan()- Deposit the ETH and nftx-vault-token’s into Uniswap for Uniswap LP tokens by calling
Uniswap.addLiquidity() - Stake the Uniswap LP tokens in
NFTXLPStakingby callingNFTXLPStaking.deposit() - 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() - Remove LP tokens from
NFTXLPStakingby callingNFTXLPStaking.exit(); - Withdraw ETH and nftx-vault-token’s by calling
Uniswap.removeLiquidity(); - Pay back nftx-vault-token flash loan
- 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.
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.
@0xKiwi do you mind showing where in code that occurs?
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
- Wait until an NFTX vault has accrued a significant amount of fees or rewards.
- Borrow a large amount of ETH from a flash loan provider.
- Borrow a large amount of nftx-vault-token using the vault's flashLoan function.
- Add liquidity to Uniswap with the borrowed ETH and nftx-vault-token to get LP tokens.
- Stake the LP tokens in NFTXLPStaking.
- 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.