Skip to content
Business logic Audit finding Intermediate

Wrong logic in AUTO_COMPOUND doesn’t allow for swap to token1

The AUTO_COMPOUND action in a smart contract has a faulty condition that prevents swapping to token1. An attacker could not exploit this, but it breaks intended protocol functionality.

Dup1337 Follow

Originally disclosed on Code4rena

Submitted by Dup1337

Protocol functionality broken

Proof of Concept

AUTO_COMPOUND action allows for compounding your gains into liquidity. It additionally allows for swaps in the middle. There is a faulty condition though, which is never effective and does not allow to set token1 as targetToken, namely else if (state.token0 == state.token1):

        } else if (params.action == Action.AUTO_COMPOUND) {
            if (params.targetToken == state.token0) {
                _swapAndIncrease(
//[...]

            // @audit it should be params.targetToken == state.token1 Currently it doesn't allow for a swap
@>          } else if (state.token0 == state.token1) {
                _swapAndIncrease(
//[...]

            } else {
                // compound without swap
                _swapAndIncrease(
//[...]
            }

Because pools cannot have the same token0 and token1, there is no possible position that will fulfill this condition. Looking at other parts of the codebase and params passed to _swapAndIncrease() in this code branch, what the protocol wants to achieve is to check if params.targetToken == state.token1 and perform swap similarly to params.targetToken == state.token0 branch.

Recommended Mitigation Steps

        } else if (params.action == Action.AUTO_COMPOUND) {
            if (params.targetToken == state.token0) {
                _swapAndIncrease(SwapAndIncreaseLiquidityParams(params.protocol, params.nfpm, params.tokenId, state.amount0, state.amount1, 0, positionOwner, params.deadline, IERC20(state.token1), params.amountIn1, params.amountOut1Min, params.swapData1, 0, 0, bytes(""), params.amountAddMin0, params.amountAddMin1, 0), IERC20(state.token0), IERC20(state.token1), false);
-           } else if (state.token0 == state.token1) {
+           } else if (params.targetToken == state.token1) {
                _swapAndIncrease(SwapAndIncreaseLiquidityParams(params.protocol, params.nfpm, params.tokenId, state.amount0, state.amount1, 0, positionOwner, params.deadline, IERC20(state.token0), 0, 0, bytes(""), params.amountIn0, params.amountOut0Min, params.swapData0, params.amountAddMin0, params.amountAddMin1, 0), IERC20(state.token0), IERC20(state.token1), false);

3docSec (judge) decreased severity to Medium and commented:

The finding completely misses a justification for High severity. A swap not happening as it should is better categorized as Medium.

Haupc (Krystal DeFi) confirmed


The original report as published on Code4rena, by Dup1337. 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 a typo in the condition: it checks if token0 equals token1 instead of checking if the target token is token1.

Impact

The swap to token1 during auto-compounding cannot occur, so the protocol does not function as intended; no direct loss of funds is described.

What the attacker needed

None

The fix

The condition should be changed to check if the target token is token1, as shown in the mitigation.

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.

This is a bug in a smart contract that handles automatic compounding of liquidity. The code checks a condition that can never be true, so a feature to swap to a certain token does not work. It is a logic error, not a security vulnerability that lets someone steal funds.

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 Dup1337

View all

3 more disclosed reports, best first.

More from Krystal DeFi Invitational

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.