Skip to content
Business logic Audit finding Intermediate

Flash swap call back prior to transferring tokens in indexPool

The flashSwap function in the IndexPool contract calls the callback before transferring tokens, so the sender does not receive tokens during the callback. This breaks the expected flash swap behaviour and could require redeploying all index pools if found after mainnet launch.

broccoli Follow

Originally disclosed on Code4rena

Submitted by broccoli, also found by 0xsanson and cmichel

Impact

In the IndexPool contract, flashSwap does not work. The callback function is called prior to token transfer. The sender won’t receive tokens in the callBack function. ITridentCallee(msg.sender).tridentSwapCallback(context);

Flashswap is not implemented correctly. It may need a migration to redeploy all indexPools if the issue is found after main-net launch. I consider this a high-risk issue.

Proof of Concept

IndexPool.sol#L196-L223

ITridentCallee(msg.sender).tridentSwapCallback(context);
// @dev Check Trident router has sent `amountIn` for skim into pool.
unchecked { // @dev This is safe from under/overflow - only logged amounts handled.
    require(_balance(tokenIn) >= amountIn + inRecord.reserve, "NOT_RECEIVED");
    inRecord.reserve += uint120(amountIn);
    outRecord.reserve -= uint120(amountOut);
}
_transfer(tokenOut, amountOut, recipient, unwrapBento);

Recommended Mitigation Steps

_transfer(tokenOut, amountOut, recipient, unwrapBento);
ITridentCallee(msg.sender).tridentSwapCallback(context);
// @dev Check Trident router has sent `amountIn` for skim into pool.
unchecked { // @dev This is safe from under/overflow - only logged amounts handled.
    require(_balance(tokenIn) >= amountIn + inRecord.reserve, "NOT_RECEIVED");
    inRecord.reserve += uint120(amountIn);
    outRecord.reserve -= uint120(amountOut);
}

maxsam4 (Sushi) commented:

Duplicate of https://github.com/code-423n4/2021-09-sushitrident-findings/issues/157 and https://github.com/code-423n4/2021-09-sushitrident-findings/issues/80

The original report as published on Code4rena, by broccoli. 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

The callback is invoked before the outbound token transfer, so the recipient cannot use the tokens during the callback.

Impact

Flash swaps do not work as intended, and fixing after mainnet launch may require migrating all index pools.

What the attacker needed

None

The fix

The outbound token transfer should be moved before the callback invocation.

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 flash swap lets someone borrow tokens and repay them within one transaction. The contract should send the borrowed tokens to the borrower before asking them to repay. Here, the contract asks for repayment first, so the borrower never gets the tokens to use.

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 broccoli

View all

16 more disclosed reports, best first.

More from Sushi Trident contest phase 1

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.