Skip to content
Race condition Audit finding Advanced

Treasury module is vulnerable to cross-contract reentrancy

The treasury module measures token balances before and after a transfer to verify loan repayment, but an attacker can trigger protocol revenue collection during the transfer, causing those tokens to be counted as repayment. This allows the attacker to reduce their loan without actually providing funds.

Czar102 Follow

Originally disclosed on Code4rena

Submitted by Czar102

https://github.com/code-423n4/2022-08-olympus/blob/main/src/modules/TRSRY.sol#L108-L112

An attacker can pay back their loan to the treasury module with protocol-owned tokens. This will cause their loan to decrease despite the protocol won’t be given funds for it.

Proof of Concept

The code first measures the number of tokens in the treasury, then transfers an amount to the contract and checks the change it caused. This is put behind a nonReentrant modifier so that one can’t use the same balance change to pay back multiple parts of (potentially) multiple loans.

The problem arises when the treasury doesn’t only claim tokens from paying back loans, but also claims protocol revenue. Since, an attacker can gain execution in the moment the funds are pulled to the treasury to trigger any function that grants treasury this type of tokens (collects protocol revenue). The contract will count these tokens as paying back one’s loan since this happened between balance measurements.

Recommended Mitigation Steps

Add a function used to pull a token to the contract and mark it nonReentrant. Any transfer of tokens to the treasury should be done through that function.

ind-igo (Olympus) commented:

I am confused by this submission. Need more information.

ind-igo (Olympus) confirmed and commented:

Spoke with Czar, solution for minimal change is adding received = min(received, amount_);. Confirming issue.


The original report as published on Code4rena, by Czar102. 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 contract uses a balance-difference check to measure repayment but does not prevent other token inflows (such as protocol revenue) from occurring during the same transaction, so those inflows are incorrectly credited to the loan.

Impact

An attacker could reduce their loan balance without providing the corresponding funds, effectively stealing from the protocol.

What the attacker needed

Attacker has an outstanding loan; protocol revenue can be collected during repayment

The fix

The fix is to cap the measured received amount at the intended repayment amount, as confirmed by the protocol team.

How it was found

  1. The attacker initiates a loan repayment to the treasury module.
  2. During the token transfer, the attacker triggers a function that collects protocol revenue into the treasury.
  3. The treasury's balance increases due to the revenue, and this increase is counted as part of the repayment.
  4. The attacker's loan is reduced by the revenue amount without the attacker providing equivalent funds.

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.

A smart contract lets you repay a loan by sending tokens. It checks how many tokens arrived by comparing the balance before and after. But if, during that same transaction, the contract also receives other tokens (like fees), those extra tokens get counted as your repayment, so your debt shrinks without you paying.

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 Czar102

View all

12 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.