LockeERC20 is vulnerable to frontrun attack
The ERC20 approve function allows a spender to front-run an allowance change and take both the old and new amounts. An attacker could steal tokens if they can front-run the victim's approve transaction.
Submitted by egjlmn1, also found by itsmeSTYJ, toastedsteaksandwich, and WatchPug
Impact
A user can steal another user’s tokens if he frontrun before he changes the allowance.
The approve() function receives an amount to change to. Lets say user A approved user B to take N tokens, and now he wants to change from N to M, if he calls approve(M) the attacker can frontrun, take the N tokens, wait until after the approve transaction, and take another M tokens. And taking N tokens more than the user wanted.
Tools Used
Manual code review
Recommended Mitigation Steps
Change the approve function to either accept the old amount of allowance and require the current allowance to be equal to that, or change to two different functions that increase and decrease the allowance instead of straight on changing it.
brockelmore (Streaming Protocol) acknowledged and disagreed with severity
Front running of the
approveERC20 function is pretty well documented and this point and there are some good ways to mitigate this risk. I am going to downgrade to Medium since there are some other requirements for this to actual mean that assets have been lost2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
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 approve function directly sets a new allowance without checking the previous value, so a spender can spend the old allowance before the change and the new allowance after it.
Impact
An attacker could steal tokens by taking both the old and new allowances, but this requires the ability to front-run the approve transaction.
What the attacker needed
Victim has an existing allowance; attacker can front-run transactions
The fix
The approve function should either require the caller to provide the current allowance and check it matches, or be replaced with separate increase and decrease functions.
How it was found
- User A approves user B to spend N tokens.
- User A decides to change the allowance to M and calls approve(M).
- An attacker front-runs the approve transaction and spends the N tokens.
- After the approve transaction is mined, the attacker spends another M tokens.
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 many tokens, a user can let someone else spend their tokens by setting an allowance. If the user later changes that allowance, an attacker who can see the pending change can quickly spend the old allowance before the change happens, then also spend the new allowance afterwards. This means the attacker gets more tokens than the user intended to allow.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.