Skip to content
Business logic Audit finding Intermediate

ChiefTrader.sol Wrong implementation of swapExactInput() and swapExactOutput()

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by Meta0xNull

When a caller calls ChiefTrader.sol#swapExactInput(), it will call ITrader(traderAddress).swapExactInput().

https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/trader/ChiefTrader.sol#L59-L59

return ITrader(traderAddress).swapExactInput(0, amount, recipient, path, options);

However, in the current implementation, inputToken is not approved to the traderAddress.

For example, in UniV3Trader.sol#_swapExactInputSingle, at L89, it tries to transfer inputToken from msg.sender (which is ChiefTrader), since it’s not approved, this will revert.

Plus, the inputToken should also be transferred from the caller before calling the subtrader.

https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/trader/UniV3Trader.sol#L89-L89

IERC20(input).safeTransferFrom(msg.sender, address(this), amount);

The same problem exists in swapExactOutput():

https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/trader/ChiefTrader.sol#L63-L75

function swapExactOutput(
    uint256 traderId,
    uint256 amount,
    address,
    PathItem[] calldata path,
    bytes calldata options
) external returns (uint256) {
    require(traderId < _traders.length, TraderExceptionsLibrary.TRADER_NOT_FOUND_EXCEPTION);
    _requireAllowedTokens(path);
    address traderAddress = _traders[traderId];
    address recipient = msg.sender;
    return ITrader(traderAddress).swapExactOutput(0, amount, recipient, path, options);
}

Recommendation

Approve the inputToken to the subtrader and transfer from the caller before calling ITrader.swapExactInput() and ITrader.swapExactOutput().

Or maybe just remove support of swapExactInput() and swapExactOutput() in ChiefTrader.

MihanixA (Mellow Protocol) confirmed:

In fact, tokens are approved to the trader https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/ERC20Vault.sol#L67 The problem is that safeTransferFrom uses msg.sender which is ChiefTrader instead of recipient which is ERC20Vault.

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.

What is known

A medium-severity business logic bug in Mellow Protocol contest. Code4rena made it public on 9 February 2022. Found in the 2021-12-mellow audit contest (finding M-04).

About Business logic

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

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

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 Mellow Protocol 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.