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

Intermediate max-input-amount check in _joinTokenSingle is unnecessary #196

Open
code423n4 opened this issue Dec 19, 2021 · 0 comments
Open
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Handle

cmichel

Vulnerability details

The SingleTokenJoin._joinTokenSingle function computes an intermediate maxAmountIn value of amountsIn[0]:

function _joinTokenSingle(JoinTokenStruct calldata _joinTokenStruct)
        internal
{
    // ... swaps to intermediate first

    path[0] = address(INTERMEDIATE_TOKEN);

    _maxApprove(INTERMEDIATE_TOKEN, address(uniSwapLikeRouter));
    for (uint256 i; i < tokens.length; i++) {
        path[1] = tokens[i];

        uint256[] memory amountsIn = uniSwapLikeRouter.getAmountsIn(
            amounts[i],
            path
        );

        uniSwapLikeRouter.swapTokensForExactTokens(
            amounts[i],
            amountsIn[0], // @audit gas: just use MAX maxAmountIn
            path,
            address(this),
            _joinTokenStruct.deadline
        );
        _maxApprove(IERC20(tokens[i]), _joinTokenStruct.outputBasket);
    }

    IBasketFacet(_joinTokenStruct.outputBasket).joinPool(
        _joinTokenStruct.outputAmount,
        _joinTokenStruct.referral
    );

    // ######## SEND TOKEN #########

    uint256 outputAmount = outputToken.balanceOf(address(this));
    require(
        outputAmount == _joinTokenStruct.outputAmount,
        "FAILED_OUTPUT_AMOUNT"
    );

    outputToken.safeTransfer(msg.sender, outputAmount);
}

It's not required to compute the input amount that is needed for the exact output swap, just do the swap and use a maxAmountIn of type(uint256).max.
The final min return check on the entire amount (outputAmount == _joinTokenStruct.outputAmount) is enough to guarantee no sandwiching.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 19, 2021
code423n4 added a commit that referenced this issue Dec 19, 2021
@loki-sama loki-sama added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 3, 2022
@loki-sama loki-sama added sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons and removed sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") labels Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

2 participants