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

Surplus token after adding liquidity is not refunded to liquidity providers. LP might suffer front-running attack and lose funds. #444

Closed
code423n4 opened this issue Dec 19, 2022 · 3 comments
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-376 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#L77

Vulnerability details

Impact

Function Pair.add() receives base token and fractional token from liquidity providers and mint equivalent amount of LP token for them.

The amount of LP token be minted is calculate in function addQuote()

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);
    }
}

As we can see, it takes the min value between baseTokenShare and fractionalTokenShare as the return value. However, the surplus token is not returned to sender but lock in the liquidity pool and become assets of all LPs, which means sender is losing funds.

Proof of Concept

Consider the scenario

  1. Currently, liquidity pool has 100 USDC - 100 FracToken. Total supply of LP token is 100.
  2. Alice want to add 50 USDC - 50 FracToken. She should get 33.33% of total supply of LP token, which is 50 LP token.
  3. Bob (attacker) front-run Alice's transaction, he swap to make the pool become 90 USDC - 111.11 FracToken.
  4. Now Alice will receive only 45 instead of 50 LP token.
baseTokenShare = 50 * 100 / 90 = 55.55
fractionalTokenShare = 50 * 100 / 111.11 = 45
lpAmount = min(baseTokenShare, fractionalTokenShare) = 45

There is 55.55 - 45 = 10.55 surplus base token share. It should be return to Alice.

Tools Used

Manual Review

Recommended Mitigation Steps

  • In Uniswap V2, users have a router to help calculate LP token amount on-chain before sending token in. So users will not send more than needed if they use router contract.
  • Since Caviar does not have router contract to support, consider returning surplus amount to LP.
@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Dec 19, 2022
code423n4 added a commit that referenced this issue Dec 19, 2022
@Shungy
Copy link
Member

Shungy commented Dec 21, 2022

Dup #90

@c4-judge
Copy link
Contributor

berndartmueller marked the issue as duplicate of #376

C4-Staff added a commit that referenced this issue Jan 6, 2023
@c4-judge
Copy link
Contributor

berndartmueller marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 10, 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 duplicate-376 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

3 participants