Skip to content
Business logic Audit finding Intermediate

initialBalance for native token is wrong

When a user swaps into the native token, the contract measures the starting balance of the wrong asset, so the computed output amount is incorrect whenever the executioner contract already holds ether. An attacker could send ether to the contract to skew that calculation, causing users to receive less than they swapped for or making their transactions fail.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by daejunpark, gpersoon, hickuphh3, kenzo, and pmerkleplant.

https://github.com/code-423n4/2021-10-slingshot/blob/9c0432cca2e43731d5a0ae9c151dacf7835b8719/contracts/Slingshot.sol#L65-L92

function executeTrades(
    address fromToken,
    address toToken,
    uint256 fromAmount,
    TradeFormat[] calldata trades,
    uint256 finalAmountMin,
    address depricated
) external nonReentrant payable {
    depricated;
    require(finalAmountMin > 0, "Slingshot: finalAmountMin cannot be zero");
    require(trades.length > 0, "Slingshot: trades cannot be empty");
    for(uint256 i = 0; i < trades.length; i++) {
        // Checks to make sure that module exists and is correct
        require(moduleRegistry.isModule(trades[i].moduleAddress), "Slingshot: not a module");
    }

    uint256 initialBalance = _getTokenBalance(toToken);
    _transferFromOrWrap(fromToken, _msgSender(), fromAmount);

    executioner.executeTrades(trades);

    uint finalBalance;
    if (toToken == nativeToken) {
        finalBalance = _getTokenBalance(address(wrappedNativeToken));
    } else {
        finalBalance = _getTokenBalance(toToken);
    }
    uint finalOutputAmount = finalBalance - initialBalance;
    ...

https://github.com/code-423n4/2021-10-slingshot/blob/9c0432cca2e43731d5a0ae9c151dacf7835b8719/contracts/Slingshot.sol#L157-L163

function _getTokenBalance(address token) internal view returns (uint256) {
    if (token == nativeToken) {
        return address(executioner).balance;
    } else {
        return IERC20(token).balanceOf(address(executioner));
    }
}

When users swap to native token (ETH), the initialBalance should use the balance of wrappedNativeToken instead of native token balance, because finalBalance is the balance of wrappedNativeToken.

In the current implementation, when the toToken is the native token, initialBalance will be the ether balance of executioner contract. Therefore, when the ether balance of executioner is not 0, finalOutputAmount will be wrong.

The attacker can transfer a certain amount of ETH to the executioner contract and malfunction the protocol. Causing fund loss to users because finalOutputAmount is lower than the actual swapped amount, or DoS due to finalAmountMin cant be met.

Proof of Concept

Given:

  • The attacker send 0.25 ETH to the executioner contract;
  • The price of ETH in USDC is: 4000
  • Alice swaps 5000 USDC to 1.25 ETH with finalAmountMin set to 1 ETH;
  • Alice will get 1 ETH out and lose 0.25 ETH;
  • Bob swaps 1000 USDC to 0.25 ETH with finalAmountMin set to 1 wei;
  • Bob’s transaction fails due to finalAmountMin cant being met.

Recommendation

Consider updating _getTokenBalance() and return IERC20(wrappedNativeToken).balanceOf(address(executioner)); when token == nativeToken.

tommyz7 (Slingshot) disagreed with severity:

“Alice swaps 5000 USDC to 1.25 ETH with finalAmountMin set to 1 ETH;” this assumption is wrong because it’s based on huge slippage assumption. There is no way a Slingshot transaction accepts 20% slippage so funds loss scenario is incorrect.

duplicate of #18, medium risk since no user funds are at risk.

The original report as published on Code4rena, by WatchPug. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the starting balance is read from the native token balance while the ending balance is read from the wrapped native token balance, so the two figures are not comparable.

Impact

Users could lose part of their swapped funds or have swaps revert, though the project argued the loss scenario relies on unrealistic slippage and rated it medium with no user funds at risk.

What the attacker needed

Attacker can send ether to the contract; victim swaps into the native token

The fix

The balance helper should be changed so that when the token is the native token it returns the wrapped native token balance of the executioner contract.

How it was found

  1. Send a small amount of ether to the executioner contract so its ether balance is not zero.
  2. Have a user swap another token into the native token with a minimum output set.
  3. Observe that the calculated output is lower than the real amount received, or that the minimum is not met and the swap fails.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A swap contract works out how much you received by comparing the balance before and after the trade. Here it looked at the wrong balance at the start, so the difference it calculated was wrong. Someone could deliberately leave extra coins in the contract to throw that maths off and harm other users.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Slingshot Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.