Write-up for PairAssetManager
Overview
This challenge questions your understanding of Uniswap v2. By solving this puzzle, you will learn about the operational logic of Uniswap v2's factory and constant product market maker (CPMM).
Solving the puzzle
The puzzle's goal is to drain both curtaUSD
and curtaStUSD
from keeper
, which is responsible for pricing the pair. The puzzle contains the following vulnerabilities you can exploit to drain the tokens:
- In
onlyUniswapV2Pair
, verification withcodeHash
can be bypassed by deployingUniswapV2Pair
directly. - In
uniswapV2Call
, ifamountIn
is greater thanmaxAmountIn
, it may not revert. - In
uniswapV2Call
, the token address passed asdata
may not be the same as the token of the called pair. - [Optional] If a token different from the initial token specified by
_createUser
in_deposit
is deposited, the share of the initial tokens pair will be increased.
In addition to the minimum requirement, vulnerability #4 enables stealing 1 ether - MINIMUM_LIQUIDITY
additional curtaUSD
and curtaStUSD
from owner
.
We can exploit these vulnerabilities as follows:
- Create a fake token and directly deploy
UniswapV2Pair
(i.e. without the factory). mint
andsync
the fake tokens with the fake pair and scale them so thatPairAssetManager._getAmountIn
of one token results in a large value.- Call
initialize
to change the tokens tocurtaUSD
andcurtaStUSD
. In the usual case where the pair is deployed through Uniswap's factory,initialize
can only be called once to create the pair, but since we deployed it directly, we can callinitialize
multiple times. - Call
swap
on theUniswapV2Pair
we deployed, and specifyto
asPairAssetManager
anddata
as the token addresses to steal + the number of tokenskeeper
has. We can drain the tokens with the large value returned by_getAmountIn
fromkeeper
. burn
all the fake tokens held by the fake pair and callsync
to set the reserve to zero.- Call
initialize
to replace the token with the fake token again, andmint
andsync
the other token so that itsPairAssetManager._getAmountIn
increases. - Repeat steps 4 and 5, and drain all tokens via
skim
.
Solve script
Check out our solve test below for more details.