Skip to content

Race condition in the ERC20 “approve” function may lead to token theft #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mgcolburn opened this issue May 7, 2020 · 0 comments

Comments

@mgcolburn
Copy link

Race condition in the ERC20 “approve” function may lead to token theft

Severity: Medium
Difficulty: High
Type: Timing

Description

A known race condition in the ERC20 standard, on the approve function, could lead to token theft. The ERC20 standard describes how to create generic token contracts. Among others, an ERC20 contract defines these two functions:

  • transferFrom(from, to, value)
  • approve(spender, value)

These functions give permission to a third party to spend tokens. Once the function approve(spender, value) has been called by a user, spender can spend up to value of the user’s tokens by calling transferFrom(user, to, value).

This schema is vulnerable to a race condition, where the user calls approve a second time on a spender that has already been allowed. If the spender sees the transaction containing the call before it has been mined, the spender can call transferFrom to transfer the previous value and still receive the authorization to transfer the new value.

Recommendation

One common workaround is to use two non-ERC20 functions, allowing a user to increase and decrease the approval (see increaseApproval and decreaseApproval of StandardToken.sol#L63-L98). Ensure users are aware of this extra functionality, and encourage them to make use of it when appropriate.

Jongsic added a commit that referenced this issue May 12, 2020
#2 Added missing events
#3 bump up compiler version to 0.5.12(same with DAI)
#6 use enum to represent contract state
#7 Added methods to prevent "approve" race condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant