`swap()` will be reverted if `path` has more tokens
The swap adapter assumed a fixed byte offset to extract the output token from encoded swap data, which only works for single-hop paths. With multi-hop paths the offset is wrong, so the swap function reverts and keepers cannot call swapForTau.
Source: https://github.com/sherlock-audit/2023-03-taurus-judging/issues/160
Found by
GimelSec, shaka
Summary
swap() will be reverted if path has more tokens, the keepers will not be able to successfully call swapForTau().
Vulnerability Detail
In test/SwapAdapters/00_UniswapSwapAdapter.ts:
// Get generic swap parameters
const basicSwapParams = buildUniswapSwapAdapterData(
["0xyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", "0xzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"],
[3000],
testDepositAmount,
expectedReturnAmount,
0,
).swapData;
We will get:
000000000000000000000000000000000000000000000000000000024f49cbca
0000000000000000000000000000000000000000000000056bc75e2d63100000
0000000000000000000000000000000000000000000000055de6a779bbac0000
0000000000000000000000000000000000000000000000000000000000000080
000000000000000000000000000000000000000000000000000000000000002b
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy000bb8zzzzzzzzzzzzzzzzzz
zzzzzzzzzzzzzzzzzzzzzz000000000000000000000000000000000000000000
Then the swapOutputToken is _swapData[length - 41:length - 21].
But if we have more tokens in path:
const basicSwapParams = buildUniswapSwapAdapterData(
["0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0xyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", "0xzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"],
[3000, 3000],
testDepositAmount,
expectedReturnAmount,
0,
).swapData;
000000000000000000000000000000000000000000000000000000024f49cbca
0000000000000000000000000000000000000000000000056bc75e2d63100000
0000000000000000000000000000000000000000000000055de6a779bbac0000
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000042
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx000bb8yyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyyyyy000bb8zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
zzzz000000000000000000000000000000000000000000000000000000000000
swapOutputToken is _swapData[length - 50:length - 30], the swap() function will be reverted.
Impact
The keepers will not be able to successfully call SwapHandler.swapForTau(). Someone will get a reverted transaction if they misuse UniswapSwapAdapter.
Code Snippet
Tool used
Manual Review
Recommendation
Limit the swap pools, or check if the balance of _outputToken should exceed _amountOutMinimum.
Discussion
Sierraescape
https://github.com/protokol/taurus-contracts/pull/82
MLON33
Fixed here
IAm0x52
Fix looks good. Byte position of outputToken is now calculated dynamically based on path length rather than a hardcoded 21 bytes which only worked for single hops
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 the output token address is extracted using a hardcoded byte offset that assumes a single-hop path, rather than being calculated from the actual path length.
Impact
Keepers cannot successfully call SwapHandler.swapForTau() when a multi-hop path is used, and any caller misusing the adapter gets a reverted transaction.
What the attacker needed
Multi-hop swap path; caller uses UniswapSwapAdapter
The fix
The byte position of the output token was changed to be calculated dynamically from the path length instead of a hardcoded 21 bytes.
How it was found
- Build swap data with a path containing three tokens and two fee tiers.
- Observe that the encoded data is longer than for a two-token path.
- Call swap() with this data and see it revert because the output token is read from the wrong offset.
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 function reads a token address from a fixed position in a blob of data. That position is only correct when the swap goes through one pool. When the swap goes through two pools, the data is longer and the fixed position points at the wrong bytes, so the function fails and the transaction is undone.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.