Skip to content
Smart contracts Audit finding Intermediate

Index Pool always swap to Zero

The _pow function in IndexPool.sol has a missing bracket for the for loop, causing the exponentiation by squaring to be incorrect. As a result, when a pool is initialised with a weight ratio of 1:2, no user can buy token A with token B because the swap always returns zero.

broccoli Follow

Originally disclosed on Code4rena

Submitted by broccoli, also found by 0xsanson, cmichel, and WatchPug

Impact

When an Index pool is initiated with two tokens A: B and the weight rate = 1:2, then no user can buy token A with token B.

The root cause is the error in pow. It seems like the dev tries to implement Exponentiation by squaring. IndexPool.sol#L286-L291

function _pow(uint256 a, uint256 n) internal pure returns (uint256 output) {
  output = n % 2 != 0 ? a : BASE;
  for (n /= 2; n != 0; n /= 2) a = a * a;
  if (n % 2 != 0) output = output * a;
}

There’s no bracket for for.

The IndexPool is not functional. I consider this is a high-risk issue.

Proof of Concept

When we initiated the pool with 2:1.

deployed_code = encode_abi(["address[]","uint136[]","uint256"], [
    (link.address, dai.address),
    (2*10**18,  10**18),
    10**13
])

No one can buy dai with link.

# (address tokenIn, address tokenOut, address recipient, bool unwrapBento, uint256 amountIn)
previous_balance = bento.functions.balanceOf(dai.address, admin).call()
swap_amount =  10**18

bento.functions.transfer(link.address, admin, pool.address, swap_amount).transact()
pool.functions.swap(
    encode_abi(
        ['address', 'address', 'address', 'bool', 'uint256'],
        [link.address, dai.address, admin, False, swap_amount]
    )
).transact()
current_balance = bento.functions.balanceOf(dai.address, admin).call()
token_received = current_balance - previous_balance
# always = 0
print(token_received)

Recommended Mitigation Steps

The brackets of for were missed.

function _pow(uint256 a, uint256 n) internal pure returns (uint256 output) {
    output = n % 2 != 0 ? a : BASE;
    for (n /= 2; n != 0; n /= 2) {
        a = a * a;
        if (n % 2 != 0) output = output * a;
    }
}

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

Likely a missing pair of curly brackets around the body of the for loop in the _pow function, so the conditional update of the output variable is not inside the loop.

Impact

The IndexPool is not functional; users cannot buy one token with the other when the weight ratio is 1:2, and swaps always return zero.

What the attacker needed

A deployed IndexPool with weight ratio 1:2; user has tokens to swap

The fix

The missing brackets around the for loop body should be added so that the output is updated inside the loop.

How it was found

  1. Deploy an IndexPool with two tokens and a weight ratio of 2:1.
  2. Attempt to swap token B for token A by transferring token B to the pool and calling swap.
  3. Check the balance of token A after the swap; it remains unchanged (zero received).

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 smart contract for a trading pool uses a maths function to calculate how many tokens you get when you swap. The function that raises a number to a power was written incorrectly because a pair of curly brackets was missing, so the calculation always gives the wrong answer. This means one direction of every trade in that pool returns zero tokens, making the pool unusable.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

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.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.