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

Target pool bandwidth incorrectly calculated when decreasing weight #29

Closed
code423n4 opened this issue Jun 6, 2023 · 5 comments
Closed
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-766 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards upgraded by judge Original issue severity upgraded from QA/Gas by judge

Comments

@code423n4
Copy link
Contributor

code423n4 commented Jun 6, 2023

Lines of code

https://github.com/code-423n4/2023-05-maia/blob/ccc9a39240dbd8eab22299737370996b2b833efd/src/ulysses-amm/UlyssesPool.sol#L269

Vulnerability details

Impact

Target pool bandwidth calculation is incorrect, which leads to incorrect pool balancing.

Proof of Concept

In setWeights of UlyssesPool.sol, the leftOverBandwidth gets added to the target poolState.bandwidth. As discussed with the Sponsor, this is incorrect because when weight is decreased, the bandwidth should decrease to distribute to the other pools.

This PoC uses a new test file: UlyssesFactoryTest.t.sol. Run the test by calling forge test --match-test testReduceWeightIncreasesBandwidth

import {Test, StdUtils} from "forge-std/Test.sol";
import {UlyssesFactory} from "@ulysses-amm/factories/UlyssesFactory.sol";
import {UlyssesPool} from "@ulysses-amm/UlyssesPool.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";

contract InvariantUlyssesFactory is Test {
    struct BandwidthState {
        uint248 bandwidth;
        uint8 weight;
        UlyssesPool destination;
    }
    UlyssesFactory ulyssesFactory;
    ERC20 token1;
    ERC20 token2;
    address alice = address(1);
    function setUp() public {
        ulyssesFactory = new UlyssesFactory(address(this));
        token1 = new MockERC20("Token1", "TK1", 18);
        token2 = new MockERC20("Token2", "TK2", 18);
        deal(address(token1), alice, 1 ether);
    }
    function testReduceWeightIncreasesBandwidth() public {
        ERC20[] memory assets = new ERC20[](4);
        assets[0] = token1;
        assets[1] = token1;
        assets[2] = token1;
        assets[3] = token1;

        uint8[][] memory weights = new uint8[][](4); // outer
        // Initialize the inner arrays
        weights[0] = new uint8[](4);
        weights[1] = new uint8[](4);
        weights[2] = new uint8[](4);
        weights[3] = new uint8[](4);

        // Pool1 weights
        weights[0][0] = 5;
        weights[0][1] = 10;
        weights[0][2] = 10;
        weights[0][3] = 10;

        // Pool2 weights
        weights[1][0] = 5;
        weights[1][1] = 10;
        weights[1][2] = 10;
        weights[1][3] = 10;

        // Pool3 weights
        weights[2][0] = 5;
        weights[2][1] = 10;
        weights[2][2] = 10;
        weights[2][3] = 10;

        // Pool4 weights
        weights[3][0] = 5;
        weights[3][1] = 10;
        weights[3][2] = 10;
        weights[3][3] = 10;
        
        
        uint256[] memory poolIds = ulyssesFactory.createPools(assets, weights, address(this));
        UlyssesPool pool1 = ulyssesFactory.pools(poolIds[0]); // id = 2

        vm.startPrank(alice);
        token1.approve(address(pool1), 0.1 ether);
        pool1.deposit(0.1 ether, alice);
        vm.stopPrank();
        
        uint256 pool1BandwidthBefore = pool1.getBandwidth(3);

        // Set weight to lower
        pool1.setWeight(3, 5);

        uint256 pool1BandwidthAfter = pool1.getBandwidth(3);
        assertEq(pool1BandwidthAfter > pool1BandwidthBefore, true);
    }
}

Tools Used

Manual

Recommended Mitigation Steps

Consider changing this line to:

poolState.bandwidth -= leftOverBandwidth.toUint248();

Assessed type

Math

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Jun 6, 2023
code423n4 added a commit that referenced this issue Jun 6, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Jul 7, 2023

trust1995 marked the issue as satisfactory

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

c4-judge commented Jul 7, 2023

trust1995 marked the issue as primary issue

@c4-judge
Copy link
Contributor

trust1995 marked the issue as duplicate of #772

@c4-judge
Copy link
Contributor

trust1995 changed the severity to 3 (High Risk)

@c4-judge c4-judge added 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Jul 11, 2023
@c4-judge
Copy link
Contributor

trust1995 marked the issue as duplicate of #766

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-766 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards upgraded by judge Original issue severity upgraded from QA/Gas by judge
Projects
None yet
Development

No branches or pull requests

2 participants