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

swapAVAXForExactTokens() Logical error #84

Closed
code423n4 opened this issue Oct 17, 2022 · 8 comments
Closed

swapAVAXForExactTokens() Logical error #84

code423n4 opened this issue Oct 17, 2022 · 8 comments
Labels
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 duplicate-469 satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-10-traderjoe/blob/main/src/LBRouter.sol#L520

Vulnerability details

Impact

In the last line, the excess money is returned to the user. But the processing is reversed amountsIn[0] -msg.value

function swapAVAXForExactTokens(
    uint256 _amountOut,
    uint256[] memory _pairBinSteps,
    IERC20[] memory _tokenPath,
    address _to,
    uint256 _deadline
)
    external
    payable
    override
    ensure(_deadline)
    verifyInputs(_pairBinSteps, _tokenPath)
    returns (uint256[] memory amountsIn)
{
    if (_tokenPath[0] != IERC20(wavax)) revert LBRouter__InvalidTokenPath(address(_tokenPath[0]));

    address[] memory _pairs = _getPairs(_pairBinSteps, _tokenPath);
    amountsIn = _getAmountsIn(_pairBinSteps, _pairs, _tokenPath, _amountOut);

    if (amountsIn[0] > msg.value) revert LBRouter__MaxAmountInExceeded(msg.value, amountsIn[0]);

    _wavaxDepositAndTransfer(_pairs[0], amountsIn[0]);

    uint256 _amountOutReal = _swapTokensForExactTokens(_pairs, _pairBinSteps, _tokenPath, amountsIn, _to);

    if (_amountOutReal < _amountOut) revert LBRouter__InsufficientAmountOut(_amountOut, _amountOutReal);

    if (msg.value > amountsIn[0]) _safeTransferAVAX(_to, amountsIn[0] - msg.value); // @audit 
}

Proof of Concept

forge test --mt testswapAVAXForExactTokens -vvvv

function testswapAVAXForExactTokens() public {
    uint256 amountOut = 1e18;

    (uint256 amountIn, ) = router.getSwapIn(pairWavax, amountOut, false);

    IERC20[] memory tokenList = new IERC20[](2);
    tokenList[0] = wavax;
    tokenList[1] = token6D;
    uint256[] memory pairVersions = new uint256[](1);
    pairVersions[0] = DEFAULT_BIN_STEP;

    vm.deal(DEV, amountIn*2);
    router.swapAVAXForExactTokens{value: amountIn+1}(amountOut, pairVersions, tokenList, DEV, block.timestamp);

    assertApproxEqAbs(token6D.balanceOf(DEV), amountOut, 13);
}

Tools Used

forge

Recommended Mitigation Steps

if (msg.value > amountsIn[0]) _safeTransferAVAX(_to,msg.value - amountsIn[0]);
@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Oct 17, 2022
code423n4 added a commit that referenced this issue Oct 17, 2022
@trust1995
Copy link

This is very far from critical vulnerability, the worse that can happen is that the TX through the router will revert if the sent amount is too high. Overly inflated.

@GalloDaSballo GalloDaSballo added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Oct 26, 2022
@GalloDaSballo
Copy link
Collaborator

Dup of #469

@GalloDaSballo GalloDaSballo added the duplicate This issue or pull request already exists label Oct 26, 2022
@GalloDaSballo
Copy link
Collaborator

L

@c4-judge c4-judge added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax downgraded by judge Judge downgraded the risk level of this issue duplicate-124 and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Nov 13, 2022
@c4-judge
Copy link
Contributor

Duplicate of #124

@Simon-Busch Simon-Busch added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value and removed downgraded by judge Judge downgraded the risk level of this issue QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax duplicate-124 labels Nov 21, 2022
@Simon-Busch
Copy link
Contributor

Reverted to M as requested by @GalloDaSballo
Duplicate of #469

@c4-judge
Copy link
Contributor

GalloDaSballo marked the issue as not a duplicate

@c4-judge c4-judge reopened this Nov 21, 2022
@c4-judge c4-judge removed the duplicate This issue or pull request already exists label Nov 21, 2022
@c4-judge
Copy link
Contributor

GalloDaSballo marked the issue as duplicate of #469

@Simon-Busch Simon-Busch added the satisfactory satisfies C4 submission criteria; eligible for awards label Dec 5, 2022
@Simon-Busch
Copy link
Contributor

Marked this issue as satisfactory as requested by @GalloDaSballo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 duplicate-469 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

5 participants