Skip to content
Race condition Audit finding Intermediate

KnightingRound tokenOutPrice changes

The buy function uses the current tokenOutPrice without allowing the buyer to specify a maximum acceptable price, so a price change by governance can be included before the buyer's transaction, causing the buyer to pay an unexpected price. An attacker could also front-run the price change to exploit this.

reassor Follow

Originally disclosed on Code4rena

Submitted by reassor, also found by cccz and cmichel

Function.buy buys the tokens for whatever price is set as tokenOutPrice. This might lead to accidental collisions or front-running attacks when user is trying to buy the tokens and his transaction is being included after the transaction of changing the price of the token via setTokenOutPrice.

Scenario:

  1. User wants to buy tokens and can see price tokenOutPrice
  2. User likes the price and issues a transaction to buy tokens
  3. At the same time CONTRACT_GOVERNANCE_ROLE account is increasing tokenOutPrice through setTokenOutPrice
  4. setTokenOutPrice transaction is included before user’s buy transaction
  5. User buys tokens with the price he was not aware of

Another variation of this attack can be performed using front-running.

Proof of Concept

Tools Used

Manual Review / VSCode

Recommended Mitigation Steps

It is recommended to add additional parameter uint256 believedPrice to KnightingRound.buy function and check if believedPrice is equal to tokenOutPrice.

GalloDaSballo (BadgerDAO) confirmed


The original report as published on Code4rena, by reassor. 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 buy function does not let the caller specify a maximum acceptable price, so it uses whatever tokenOutPrice is current at execution time.

What the attacker needed

User has tokens to spend; Governance changes price concurrently; or attacker front-runs

The fix

The buy function should accept a believedPrice parameter and revert if it does not match the current tokenOutPrice.

How it was found

  1. User sees the current tokenOutPrice and decides to buy tokens.
  2. User sends a transaction to buy tokens.
  3. At the same time, a governance account sends a transaction to increase tokenOutPrice.
  4. The price change transaction is included before the user's buy transaction.
  5. The user's buy executes at the new, higher price.

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 sells tokens at a price that the contract owner can change. When you send a buy request, you don't say what price you expect, so if the owner changes the price just before your request is processed, you end up paying the new price without knowing. This is like agreeing to buy something at the price on the shelf, but the shop changes the price tag just as you reach the till.

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 reassor

View all

5 more disclosed reports, best first.

More from Badger Citadel 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.