Skip to content
Race condition Audit finding Intermediate

Approve and transferFrom functions of Pool tokens are subject to front-run attack

The approve function of Pool tokens overwrites the current allowance without checking if the spender already used it, allowing a spender to front-run a change in allowance and transfer both the old and new amounts. An attacker could thus transfer more tokens than the owner intended.

0xhacksmithh Follow with 0xnuel, ABA and 18 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-surge-judging/issues/154

Found by

0xhacksmithh, 0xnuel, ABA, Cryptor, Delvir0, Dug, Handle, Kaiziron, MalfurionWhitehat, RaymondFam, Respx, Tomo, Tricko, ahmedovv, ast3ros, bytes032, dipp, kiki_dev, menox, weeeh_, wzrdk3lly

Summary

Approve and transferFrom functions of Pool tokens are subject to front-run attack because the approve method overwrites the current allowance regardless of whether the spender already used it or not. In case the spender spent the amonut, the approve function will approve a new amount.

Vulnerability Detail

The approve method overwrites the current allowance regardless of whether the spender already used it or not. It allows the spender to front-run and spend the amount before the new allowance is set.

Scenario:

  • Alice allows Bob to transfer N of Alice's tokens (N>0) by calling the pool.approve method, passing the Bob's address and N as the method arguments
  • After some time, Alice decides to change from N to M (M>0) the number of Alice's tokens Bob is allowed to transfer, so she calls the pool.approve method again, this time passing the Bob's address and M as the method arguments
  • Bob notices the Alice's second transaction before it was mined and quickly sends another transaction that calls the pool.transferFrom method to transfer N Alice's tokens somewhere
  • If the Bob's transaction will be executed before the Alice's transaction, then Bob will successfully transfer N Alice's tokens and will gain an ability to transfer another M tokens

Before Alice noticed that something went wrong, Bob calls the pool.transferFrom method again, this time to transfer M Alice's tokens.

  • So, an Alice's attempt to change the Bob's allowance from N to M (N>0 and M>0) made it possible for Bob to transfer N+M of Alice's tokens, while Alice never wanted to allow so many of her tokens to be transferred by Bob.

Impact

It can result in losing pool tokens of users when he approve pool tokens to any malicious account.

Code Snippet

https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L284 https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L299

Tool used

Manual Review

Recommendation

Use increaseAllowance and decreaseAllowance instead of approve as OpenZeppelin ERC20 implementation. Please see details here:

https://forum.openzeppelin.com/t/explain-the-practical-use-of-increaseallowance-and-decreaseallowance-functions-on-erc20/15103/4

Discussion

xeious

Fixed

xeious

https://github.com/Surge-fi/surge-protocol-v1/commit/1d1c412cf78d9c8515e621d5aabd400402a24035

IAm0x52

Fix looks good. Functions to increase or decrease approval have been added to Pool.sol

The original report as published on Sherlock, by 0xhacksmithh. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 the allowance to a new value without considering the existing allowance, enabling a front-running attack when the allowance is changed.

Impact

It can result in losing pool tokens of users when they approve pool tokens to a malicious account.

What the attacker needed

Victim has approved a malicious spender; spender can front-run transactions

The fix

The fix was to add functions to increase or decrease approval instead of using approve directly.

How it was found

  1. Alice calls pool.approve to allow Bob to transfer N of her tokens.
  2. Later, Alice calls pool.approve again to change Bob's allowance from N to M.
  3. Bob sees Alice's second transaction before it is mined and quickly calls pool.transferFrom to transfer N tokens.
  4. If Bob's transaction executes first, he transfers N tokens and still has an allowance of M.
  5. Bob then calls pool.transferFrom again to transfer M tokens, resulting in a total of N+M tokens transferred.

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.

When you approve someone to spend your tokens, the contract simply replaces the old permission with the new one. If you try to change the amount, the person you approved can quickly spend the old amount before your change goes through, and then also spend the new amount. This means they can take more of your tokens than you wanted.

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.

More by 0xhacksmithh

View all

2 more disclosed reports, best first.

More from Surge

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.