UniswapV3’s path issue for swapExactOutput
The `_reverseBytes` function reverses the entire encoded path byte-by-byte, which corrupts token addresses and breaks multi-hop swaps in `swapExactOutput`. As a result, the function cannot be used for multi-hop swaps.
Submitted by cmichel
UniswapV3 expects a path object like (tokenA, feeAB, tokenB, feeBC, tokenC). The UniV3Trader.swapExactOutput code tries to reverse this path to get to (tokenC, feeBC, tokenB, feeAB, tokenA) but that’s not what the _reverseBytes function does. Note that it reverts the entire encoded path byte array byte-by-byte which breaks the byte-order in a token. For example, tokenA would have every single byte reversed and lead to a different token.
function _reverseBytes(bytes memory input) internal pure returns (bytes memory output) {
/** @audit reverses byte order? */
for (uint256 i = 0; i < input.length; ++i) output[i] = input[input.length - 1 - i];
}
Impact
The UniV3Trader.swapExactOutput function with multi-hops is broken and cannot be used.
Recommended Mitigation Steps
Don’t reverse the path byte-by-byte but element-by-element.
0xleastwood (judge) commented:
Can you confirm if this issue is valid or not? @MihanixA
0xleastwood (judge) commented:
I actually agree with the warden’s finding here. Leaving as is.
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 `_reverseBytes` function reverses the entire byte array instead of reversing the path elements (tokens and fees) individually.
What the attacker needed
None
The fix
The path should be reversed element-by-element, not byte-by-byte.
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.
In a multi-hop swap, tokens are swapped through several intermediate tokens. The code needs to reverse the order of the tokens in the path, but it reverses the bytes of the whole path instead, which scrambles the token addresses. This makes the function unusable for multi-hop swaps.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.