Pending Apecoin staking reward does not count towards the account health
Proof of Concept
The account health factor is important because it is used to determine how much user can borrow and when the price drops, it is used to determine if the liquidation is allowed.
Let us look into the health factor calculation in GenericLogic
this function is very long, but on the high level, what is does is that:
loop over all ERC20 collateral and calculate the borrow amount, the asset worth.
loop over ERC721 token including Uniswap V3 NFT and other NFT supported.
the list of NFT supported according to the documentation is:
basically user stake their ape with ape coin for reward and they create a sell order to sell the NFT in NFT marketplace.
Another user happily step in, he first borrow flashloan, buy the NFT, claim the ape coin, then sold apecoin + the NFT for profits and repay the flashloan. It is user that create the sell order suffers: he loses the apecoin and the staking reward.
Now consider this case in the current paraspace account health factor implementation:
A User have a bored APE NFT, worth 80 ETH
He stakes the ape coin for his NFT, the ape coin staked worth 20 ETH.
The User use the bored APE NFT as collateral in the paraspace system and borrow 30 ETH.
The bored APE NFT price drops to 10 ETH, the system thinks the loan backed by the NFT is bad debt and the NFT is liquidated.
However, when the APE NFT worth NFT, his staked apecoin + the staking reward can worth 50 ETH, without counting the pending staking reward of the apecoin into the account health factor, the user's NFT is liquidated and the user lose his NFT + the apecoin.
Tools Used
Manual Review.
Recommended Mitigation Steps
We recommend the project let pending Apecoin staking reward count towards the total collateral value when calculating account health factor.
Lines of code
https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/protocol/pool/PoolApeStaking.sol#L428 https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/protocol/pool/PoolApeStaking.sol#L439 https://github.com/code-423n4/2022-11-paraspace/blob/c6820a279c64a299a783955749fdc977de8f0449/paraspace-core/contracts/protocol/libraries/logic/GenericLogic.sol#L74
Vulnerability details
Impact
Pending Apecoin staking reward does not count towards the account health
Proof of Concept
The account health factor is important because it is used to determine how much user can borrow and when the price drops, it is used to determine if the liquidation is allowed.
Let us look into the health factor calculation in GenericLogic
this function is very long, but on the high level, what is does is that:
loop over all ERC20 collateral and calculate the borrow amount, the asset worth. loop over ERC721 token including Uniswap V3 NFT and other NFT supported.
the list of NFT supported according to the documentation is:
https://docs.para.space/para-space/introduction-to-paraspace/supplying-nfts/nft-utility-and-delegated-rights#what-other-benefits-does-paraspace-offer-to-nft-holders
Bored Apt, MoonBird, CryptoPunk, Mutant Ape, Doodles, Otherdeed, Clone x and meetbits.
The parapspace also support ape coin staking optimization.
but here is the issue: the code above that calculate the account health factor does not count the pending apecoin staking reward.
When apecoin launch the staking, this happens:
https://twitter.com/peckshieldalert/status/1599961388298166272
basically user stake their ape with ape coin for reward and they create a sell order to sell the NFT in NFT marketplace.
Another user happily step in, he first borrow flashloan, buy the NFT, claim the ape coin, then sold apecoin + the NFT for profits and repay the flashloan. It is user that create the sell order suffers: he loses the apecoin and the staking reward.
Now consider this case in the current paraspace account health factor implementation:
Tools Used
Manual Review.
Recommended Mitigation Steps
We recommend the project let pending Apecoin staking reward count towards the total collateral value when calculating account health factor.