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

Wrong token amount is transferred when calling add() of contract Pair #388

Closed
code423n4 opened this issue Dec 19, 2022 · 3 comments
Closed
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#L85
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L93-L96

Vulnerability details

Impact

Users calling Pair.sol#add() will cost more tokens than needed.
It also makes Pair.sol#add() and Pair.sol#nftAdd() vulnerable to sandwich attacks.

Proof of Concept

It is incorrect that the token amounts transferred in function Pair.sol#add is derived from input parameters (baseTokenAmount, fractionalTokenAmount) rather than recalculate the amounts from the lpTokenAmount to mint:

  function add(uint256 baseTokenAmount, uint256 fractionalTokenAmount, uint256 minLpTokenAmount) returns (uint256 lpTokenAmount) {
      // -- omit --
      lpTokenAmount = addQuote(baseTokenAmount, fractionalTokenAmount);
      // -- omit --
      _transferFrom(msg.sender, address(this), fractionalTokenAmount);
      // -- omit --
      lpToken.mint(msg.sender, lpTokenAmount);
      // -- omit --
      if (baseToken != address(0)) {
          ERC20(baseToken).safeTransferFrom(msg.sender, address(this), baseTokenAmount);
      }
      // -- omit --
  }

For example, if a pair contains 10 ETH and 1000 FT(fractional token), the following three users will get the same amount of lp tokens:

  1. user1: add(1 ETH, 100 FT)
  2. user2: add(1 ETH, 200 FT)
  3. user3: add(11 ETH, 100 FT)

All of them will get the same number of lp tokens, which means user2 costs 100 FT more and user3 costs 10 ETH more.

Tools Used

Manual

Recommended Mitigation Steps

  1. calculate the amount of fractional tokens to transferFrom based on the lpTokenAmount
  2. if base token is ERC20: calculate the amount of base tokens to transferFrom based on the lpTokenAmount
  3. if base token is ETH: calculate the amount of base tokens needed based on the lpTokenAmount and return the excess ETH to user.
@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 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-376 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

3 participants