Skip to content
Business logic Audit finding Intermediate

zeroswap/UniswapV2Library.sol Wrong init code hash in UniswapV2Library.pairFor() will break UniswapV2Oracle , UniswapV2Router02 , SushiRoll

The UniswapV2Library.pairFor() function used a hardcoded init code hash that no longer matched the modified UniswapV2Pair contract, so it calculated wrong pair addresses. This caused contracts like UniswapV2Oracle, UniswapV2Router02, and SushiRoll to malfunction, breaking swapping functionality.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/Plex-Engineer/zeroswap/blob/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/libraries/UniswapV2Library.sol#L20-L28

function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
    (address token0, address token1) = sortTokens(tokenA, tokenB);
    pair = address(uint(keccak256(abi.encodePacked(
            hex'ff',
            factory,
            keccak256(abi.encodePacked(token0, token1)),
            hex'e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303' // init code hash
        ))));
}

The init code hash in UniswapV2Library.pairFor() should be updated since the code of UniswapV2Pair has been changed. Otherwise, the pair address calculated will be wrong, most likely non-existing address.

There are many other functions and other contracts across the codebase, including UniswapV2Oracle, UniswapV2Router02, and SushiRoll, that rely on the UniswapV2Library.pairFor() function for the address of the pair, with the UniswapV2Library.pairFor() returning a wrong and non-existing address, these functions and contracts will malfunction.

Recommended Mitigation Steps

Update the init code hash from hex'e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303' to the value of UniswapV2Factory.pairCodeHash().

tkkwon1998 (Canto) confirmed

Alex the Entreprenerd (judge) commented:

Amazing catch, because the contract bytecode has been change, the init hash will be different.

While the bug seems trivial, it’s impact is a total bricking of all swapping functionality as the Library will cause all Periphery Contracts to call to the wrong addresses.

Because of the impact, I agree with High Severity.


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

The init code hash constant in UniswapV2Library.pairFor() was not updated after the UniswapV2Pair contract bytecode changed.

Impact

All swapping functionality could be bricked because periphery contracts would call wrong, non-existing addresses.

What the attacker needed

None

The fix

The init code hash should be updated to the value returned by UniswapV2Factory.pairCodeHash().

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.

Smart contracts on Ethereum have a unique address. A library function tried to predict a pair contract's address using a fixed fingerprint (hash) of the pair's code. Because the pair's code was changed, the fingerprint was outdated, so the predicted address was wrong and pointed to nothing. Other contracts relying on this prediction then failed to work.

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 Canto

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.