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

First LP can manipulate the pricing #121

Closed
code423n4 opened this issue Dec 16, 2022 · 2 comments
Closed

First LP can manipulate the pricing #121

code423n4 opened this issue Dec 16, 2022 · 2 comments
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-442 satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L417-L428
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L63-L99

Vulnerability details

Impact

The first LP can impact the pricing formula used in addQuote function . It will make the pricing of baseTokenShare and fractionalTokenShare favourable to the second LP.

Proof of Concept

Example:

The add function of Pair takes baseTokenAmount and fractionalTokenAmount as parameter.
The first LP front runs the Pair and sets
baseTokenAmount = 1;
fractionalTokenAmount =1;

Now see the addQuote function

function addQuote(uint256 baseTokenAmount, uint256 fractionalTokenAmount) public view returns (uint256) {
        uint256 lpTokenSupply = lpToken.totalSupply();
        if (lpTokenSupply > 0) {
            // calculate amount of lp tokens as a fraction of existing reserves
            uint256 baseTokenShare = (baseTokenAmount * lpTokenSupply) / baseTokenReserves();
            uint256 fractionalTokenShare = (fractionalTokenAmount * lpTokenSupply) / fractionalTokenReserves();
            return Math.min(baseTokenShare, fractionalTokenShare);
        } else {
            // if there is no liquidity then init
            return Math.sqrt(baseTokenAmount * fractionalTokenAmount);
        }
    }

Here,
as lpTokenSupply ==0 , it will return 1 ;
Now lpTokenSupply will return 1 for second LP.

Second LP calls the add function again ,
In the addQuote function ,

baseTokenShare will be equal to the baseTokenAmount and similarly , fractionalTokenShare = fractionalTokenAmount

This phenomenon , ignores the other 2 variables in the formula( lpTokenSupply and baseTokenReserves ,fractionalTokenReserves ) , as they are 1 in value .
Thus, it will be more advantageous for the second LP to mint the tokens, which will put , the rest of the future LPs at a disadvantage.

Tools Used

Manual

Recommended Mitigation Steps

  • Mint some initial LP tokens to the Zero address in the constructor .
  • In the addQuote function, when lpTokenSupply ==0 , enforce a require statement , to make the user deposit a minimum amount of baseTokenAmount that will make the calculation more robust to manipulation.
@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Dec 16, 2022
code423n4 added a commit that referenced this issue Dec 16, 2022
@c4-judge
Copy link
Contributor

berndartmueller marked the issue as duplicate of #442

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 10, 2023
@c4-judge
Copy link
Contributor

berndartmueller marked the issue as satisfactory

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 duplicate-442 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants