Skip to content
Race condition Audit finding Intermediate

TRSRY: front-runnable setApprovalFor

The treasury module's setApprovalFor function directly overwrites the approval amount, allowing a withdrawer to front-run a reduction and withdraw the old larger amount before the new smaller approval takes effect. An attacker could thus withdraw more than intended, up to the previous approval amount.

zzzitron Follow

Originally disclosed on Code4rena

Submitted by zzzitron, also found by berndartmueller, csanuragjain, pashov, Ruhum, sorrynotsorry, and Trust

https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/modules/TRSRY.sol#L64-L72 https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/policies/TreasuryCustodian.sol#L42-L48

An attacker may be able to withdraw more than intended

Proof of Concept

Let’s say Alice had approval of 100. Now the treasury custodian reduced the approval to 50. Alice could frontrun the setApprovalFor of 50, and withdraw 100 as it was before. Then withdraw 50 with the newly set approval. So the alice could withdraw 150.

// modules/TRSRY.sol

 63     /// @notice Sets approval for specific withdrawer addresses
 64     function setApprovalFor(
 65         address withdrawer_,
 66         ERC20 token_,
 67         uint256 amount_
 68     ) external permissioned {
 69         withdrawApproval[withdrawer_][token_] = amount_;
 70
 71         emit ApprovedForWithdrawal(withdrawer_, token_, amount_);
 72     }

The TreasuryCustodian simply calls the setApprovalFor to grant Approval.

 41
 42     function grantApproval(
 43         address for_,
 44         ERC20 token_,
 45         uint256 amount_
 46     ) external onlyRole("custodian") {
 47         TRSRY.setApprovalFor(for_, token_, amount_);
 48     }

Recommended Mitigation Steps

Instead of setting the given amount, one can reduce from the current approval. By doing so, it checks whether the previous approval is spend.

ind-igo (Olympus) confirmed and commented:

Understood. Will change the logic to increase/decrease allowances.

0xean (judge) increased severity to High and commented:

I think this vulnerability should be a high severity as it opens up the possibility of a direct loss of funds in the amount of up to the previous approval amount. Upgrading to High.

0xean (judge) commented:

@ind-igo - Not sure if you deleted your comment, but that context is useful. Happy to take another look here.

ind-igo (Olympus) commented:

I did, I just thought it was unnecessary to evaluate the issue. I was just saying that the context of the code is that it is not intended to be used to approve an EOA/multisig, but instead used to approve governance-voted contracts to access treasury funds, in order to deposit into yield contracts or whatever. But I don’t think it’s very relevant to this, as the code is still faulty and exploitable in an extreme case. I already have made this remediation as well, so all good.


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

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 setApprovalFor function sets the approval amount directly instead of adjusting it relative to the current allowance, so a reduction does not immediately invalidate the previous allowance.

Impact

An attacker could withdraw up to the previous approval amount in addition to the new amount, leading to a direct loss of funds.

What the attacker needed

Attacker has an existing approval; attacker can front-run transactions

The fix

The logic should be changed to increase or decrease allowances relative to the current approval, rather than setting a new absolute value.

How it was found

  1. Alice has an existing approval of 100.
  2. The treasury custodian calls setApprovalFor to reduce Alice's approval to 50.
  3. Alice front-runs the reduction transaction and withdraws 100 using the old approval.
  4. After the reduction is confirmed, Alice withdraws another 50 using the new approval.

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 smart contracts, an approval lets someone else spend your tokens. If the approval is changed by simply setting a new number, a fast attacker can spend the old larger amount before the change is processed, then also spend the new amount. This is like changing a spending limit but the old limit still works for a moment.

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 zzzitron

View all

21 more disclosed reports, best first.

More from Olympus DAO contest

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.