sherlock-audit / 2024-02-telcoin-platform-audit-update-judging

3 stars 1 forks source link

cheatcode - Sandwich Attack Vulnerability in AmirX::stablecoinSwap function #81

Closed sherlock-admin4 closed 8 months ago

sherlock-admin4 commented 8 months ago

cheatcode

medium

Sandwich Attack Vulnerability in AmirX::stablecoinSwap function

Summary

The AmirX contract is vulnerable to sandwich attacks during stablecoin swaps and DeFi swap operations, potentially allowing attackers to exploit the order of transactions for profit.

Vulnerability Detail

The stablecoinSwap function in the AmirX contract facilitates stablecoin swaps and triggers DeFi swap operations based on the provided StablecoinSwap and DefiSwap structs. However, this function lacks proper safeguards against sandwich attacks, making it susceptible to front-running and back-running attacks by malicious actors.

function stablecoinSwap(
        address wallet,
        address safe,
        StablecoinSwap memory ss,
        DefiSwap memory defi
    ) external payable onlyRole(SWAPPER_ROLE) {
        // checks if it will fail
        _verifyStablecoin(wallet, safe, ss, defi);

        //eXYZ ot eXYZ
        if (isXYZ(ss.origin) && isXYZ(ss.target)) {
            swapAndSend(wallet, ss);
            return;
        }

        //stablecoin swap
        if (isXYZ(ss.origin) && !isXYZ(ss.target))
            convertFromEXYZ(wallet, safe, ss);

        //defi swap
        uint256 iBalance = ERC20(ss.origin).balanceOf(wallet);
        if (defi.walletData.length != 0) defiSwap(wallet, safe, defi);
        uint256 fBalance = ERC20(ss.origin).balanceOf(wallet);
        // //stablecoin swap
        if (!isXYZ(ss.origin) && isXYZ(ss.target)) {
            if (fBalance - iBalance != 0) ss.oAmount = fBalance - iBalance;
            convertToEXYZ(wallet, safe, ss);
        }
    }

Impact

Attacker is able to exploit user transactions for profit, potentially resulting in significant financial losses for users executing stablecoin swaps or DeFi swap operations through the AmirX contract.

Code Snippet

https://github.com/sherlock-audit/2024-02-telcoin-platform-audit-update/blob/main/telcoin-contracts/contracts/swap/AmirX.sol#L66

Tool used

Manual Review

Recommendation

  1. Allow users to specify a maximum allowed slippage for their swaps, reverting transactions if the slippage exceeds the specified threshold due to front-running.

  2. Implement measures to minimize the predictability of transaction ordering, such as using commit-reveal schemes.

sherlock-admin2 commented 8 months ago

1 comment(s) were left on this issue during the judging contest.

WangAudit commented:

extremely vague; tell me exactly how it can be done; otherwise it's just an assumption with no proof