Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owner's ability to set any arbitrary virtualReserves allowes him to steal users funds #829

Closed
code423n4 opened this issue Apr 13, 2023 · 7 comments
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working judge review requested Judge should review this issue primary issue Highest quality submission among a set of duplicates sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue unsatisfactory does not satisfy C4 submission criteria; not eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L538-L545

Vulnerability details

Impact

The owner of PrivatePool has the ability to set the virtualReserve to any arbitrary value he wants. There is no upper or lower limit.

When the user makes a call to buy() or sell() of PrivatePool, the owner can sandwich the user's transaction and set the virtualReserves in such a way that,
-1. The user might have to pay a very big amount of baseToken to buy the NFTs
-2. The user might not receive any baseToken in return while selling the NFTs

In EthRouter.sol, there is slippage protection for buy() and sell(), but the owner can sandwich attack the user's transaction, making sure the user will receive or pay the limit he provided in the argument.

Proof of Concept

Assume,
Current virtualBaseTokenReserves = 100e18, virtualNftReserves = 40e18
-1. Alice calls buy() of PrivatePool with tokenWeights = 10e18,
-2. The attacker front-run her transaction and calls setVirtualReserves(), with a very high value of virtualBaseTokenReserves or a very low value of virtualNftReserves or both. There is no limit to function.
- virtualBaseTokenReserves = 1000e18
- virtualNftReserves = 11e18
https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L538-L545
-3. Alice's transaction goes through
- In buy, inputAmount is calculated using the following formula,
https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L694-L706
- inputAmount = (10e18 * 1000e18) / (11e18 - 10e18)
- inputAmount = 10000e18
- So, inputAmount would be very high instead it should have been 33.33e18 according to actual virtualreserve
- Remember, Owner can always check user's balance and manipulate the price accordingly
-4. Owner set the virtualReserve to normal range

During sell(), the Owner can manipulate the reserve in such a way that the user will not receive any baseToken in return for their NFTs by lowering the virtualBaseTokenReserves or increasing the virtualNftReserves
https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L713-L724

Tools Used

Manual Review

Recommended Mitigation Steps

There should be some limit by which the owner can manipulate the reserve. Ideally it should follow xy=k

@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Apr 13, 2023
code423n4 added a commit that referenced this issue Apr 13, 2023
@c4-pre-sort
Copy link

0xSorryNotSorry marked the issue as primary issue

@c4-pre-sort c4-pre-sort added the primary issue Highest quality submission among a set of duplicates label Apr 20, 2023
This was referenced Apr 20, 2023
@outdoteth
Copy link

Sandwich attack is a known risk and is documented in the code with a comment that even warns against it: https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L204. The slippage check in the router contract serves to protect against this. Sandwiching is an issue in almost all AMM designs. I'm not sure if this is valid or not.

@c4-sponsor
Copy link

outdoteth requested judge review

@c4-sponsor c4-sponsor added the judge review requested Judge should review this issue label Apr 21, 2023
@c4-sponsor
Copy link

outdoteth marked the issue as sponsor acknowledged

@c4-sponsor c4-sponsor added the sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons label Apr 21, 2023
@c4-sponsor
Copy link

outdoteth marked the issue as sponsor disputed

@c4-sponsor c4-sponsor added sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue and removed sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons labels Apr 22, 2023
@GalloDaSballo
Copy link

The router protects against this

@c4-judge
Copy link
Contributor

GalloDaSballo marked the issue as unsatisfactory:
Invalid

@c4-judge c4-judge added the unsatisfactory does not satisfy C4 submission criteria; not eligible for awards label Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working judge review requested Judge should review this issue primary issue Highest quality submission among a set of duplicates sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue unsatisfactory does not satisfy C4 submission criteria; not eligible for awards
Projects
None yet
Development

No branches or pull requests

6 participants