Approve and transferFrom functions of WToken are subject to front-run attack.It is recommended to use increaseAllowance
and decreaseAllowance
as OpenZeppelin ERC20 implementation.ERC20 API: An Attack Vector on the Approve/TransferFrom Methods.
Changing an allowance with approve brings the risk that someone may use both the old and the new allowance by unfortunate transaction ordering.
The attack scenario is as follows:
Holder allows spender
to transfer N of holder's tokens (N > 0)by calling the approve method on WToken smart contract, passing the spender's address and N as the method arguments
After some time, holder decides to change from N to M (M > 0) (1000 to 300) the number of holder's tokens spender
is allowed to transfer, so he calls the approve
method again, this time passing the spender
's address and M as the method arguments
Spender
notices the holder's second transaction before it was mined and quickly sends another transaction that calls the transferFrom
method to transfer N holder's tokens somewhere
If the spender
's transaction will be executed before the holder's transaction, then spender
will successfully transfer
N holder's tokens and will gain an ability to transfer another M tokens
Before holder noticed that something went wrong, spender
calls the transferFrom
method again, this time to transfer
M Holder's tokens.
So, an holder's attempt to change the spender
's allowance from N to M (N=1000
and M=300
) made it possible for spender
to transfer N+M
of holder's tokens, while holder never wanted to allow so many of his tokens to be transferred by spender.
Foundry
Execute the following command:
result:
Use increaseAllowance
and decreaseAllowance
instead of approve as OpenZeppelin ERC20 implementation.
https://forum.openzeppelin.com/t/explain-the-practical-use-of-increaseallowance-and-decreaseallowance-functions-on-erc20/15103/4
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.