Skip to content
Race condition Audit finding Advanced

The contract serves as a flashloan pool without fee

The PuttyV2 contract allows a malicious contract to act as both maker and taker in a fillOrder, using a custom ERC20 token to pay the fee and reentering to exercise an option, thereby obtaining assets without paying a real fee. This enables flashloan-like access to the contract's assets for profit.

0xc0ffEE Follow

Originally disclosed on Code4rena

Submitted by 0xc0ffEE, also found by horsefacts, pedroais, and unforgiven

The malicious user could leverage PuttyV2 contract to flashloan without paying fee the assets to make profit.

Consider a scenario that maker and taker is the same, and is a contract

  1. The contract call PuttyV2.fillOrder with a Long Call order that has order.baseAssets references to a contract having custom logic other than standard ERC20. The order also specify erc20Assets to the token and tokenAmount that PuttyV2 contract is owing (similar to erc721Assets)
  2. When the execution is at https://github.com/code-423n4/2022-06-putty/blob/3b6b844bc39e897bd0bbb69897f2deff12dc3893/contracts/src/PuttyV2.sol#L324, the custom logic could execute on the contract address order.baseAsset.
  3. The malicious contract then call exercise to exercise the short call position. This call will transfer out the assets specified in the order to the malicious contract by executing logics in _transferERC20sOut, _transferERC721sOut
  4. The contract uses that assets to make profit on other platforms. After that, the execution continues at https://github.com/code-423n4/2022-06-putty/blob/3b6b844bc39e897bd0bbb69897f2deff12dc3893/contracts/src/PuttyV2.sol#L324.
  5. At the end of fillOrder, the contract just transfers enough assets back to PuttyV2 by executing logics in _transferERC20sIn, _transferERC721sIn to finish the execution.

Alex the Entreprenerd (warden) commented:

Warden is saying that they can flashloan without fee, but any exercised option will pay a 3% fee, additionally the order of operations shown (gain control on base.asset.transfer when receiving premium), would mean that the order ERC20s and NFTs have yet to be transferred in, so a “mid-fillOrder” “exercise” would not only pay the fee, but also revert due to lack of the tokens.

Pedroais (warden) commented:

The 3% will be paid in the fake asset since base asset is an attacker contract so there is no fee to perform the attack.

This attack is done with assets that are already inside the contract so there is no revert in transfer out.

outdoteth (Putty Finance) acknowledged and commented:

Acknowledging that technically this is true. Although no easy mitigation exists as far as I can see aside from adding nonReentrant to exercise and fillOrder - adding a non-negligible gas overhead.

Alex the Entreprenerd (warden) commented:

I agree that the finding is valid, the fee can be paid in a mintable token to gain temporary ownership of a token underlying which is repaid at the end of fillOrder.

outdoteth (Putty Finance) commented:

Report: It’s possible to flashloan all assets in the contract without paying a protocol fee.

HickupHH3 (judge) commented:

Flash loans from the contract would be a feature, not a bug. However, being able to do so without paying a protocol fee (ie. paying in fake tokens) wouldn’t be great.


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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

Putty is an on-chain protocol for options on ERC-721 tokens. Its core contract, PuttyV2, lets a maker write an option and a taker fill it. The contract holds assets belonging to open positions, and fillOrder transfers assets in and out during settlement.

The finding here is about that asset movement: the contract can be made to serve as a flashloan pool, handing out the assets it holds for the duration of a single transaction, without the protocol fee being paid in anything of value.

The report was filed against the Code4rena audit of Putty (2022-06). The same issue was also found by horsefacts, pedroais and unforgiven. It was rated medium.

Setup

The attack needs no special tooling beyond the ability to deploy a contract. The preconditions described in the report are:

  1. The maker and the taker of the order are the same party, and that party is a contract.
  2. The order is a Long Call order whose order.baseAssets points at a contract with custom logic rather than a standard ERC20.
  3. The order also specifies erc20Assets with a token and tokenAmount that PuttyV2 is owing, in the same way the order can specify erc721Assets.

Finding it

The researcher noticed that fillOrder calls out to order.baseAsset partway through its execution, at line 324 of PuttyV2.sol. Because baseAsset can be an arbitrary contract, that call is a point where attacker-controlled code runs while fillOrder is still in progress and while the contract's assets are still in place. From there the attacker contract can call exercise on a short call position and pull assets out of PuttyV2 before fillOrder has finished settling.

Exploitation

Step 1: fill a Long Call order with a malicious base asset

The attacker contract calls PuttyV2.fillOrder with a Long Call order. The order's baseAssets reference a contract with custom logic instead of a standard ERC20. The order also sets erc20Assets to the token and tokenAmount that PuttyV2 is owing, mirroring how erc721Assets works.

Step 2: gain control during fillOrder

Execution reaches the external call at:

Text
https://github.com/code-423n4/2022-06-putty/blob/[redacted]/contracts/src/PuttyV2.sol#L324

At that point the custom logic in the attacker's contract runs, with order.baseAsset as the contract address being called.

Step 3: exercise the short call position

The malicious contract calls exercise to exercise the short call position. That call transfers the assets named in the order out to the malicious contract, through the logic in _transferERC20sOut and _transferERC721sOut.

Step 4: use the assets elsewhere

The contract uses the borrowed assets to make a profit on other platforms. Execution then returns to the same point in fillOrder at line 324.

Step 5: repay at the end of fillOrder

At the end of fillOrder, the contract transfers enough assets back to PuttyV2 through _transferERC20sIn and _transferERC721sIn, and the execution completes.

Root cause

fillOrder performs an external call to order.baseAsset while it is mid-flight, and exercise can be called reentrantly from inside that call. Putty Finance noted that no easy mitigation exists apart from adding nonReentrant to exercise and fillOrder, so the attacker can take assets out during the window and put them back before fillOrder finishes.

The fee is the second half of the problem. As pedroais put it, the 3% is paid in the fake asset, so there is no real fee to perform the attack. The assets moved out are already inside the contract, so the transfer out does not revert.

The wardens went back and forth on whether the sequence works at all. Alex the Entreprenerd first argued that an exercised option pays a 3% fee and that a mid-fillOrder exercise would revert for lack of tokens, because the order's ERC20s and NFTs had not yet been transferred in. Pedroais answered that the fee is paid in the fake asset and that the assets are already in the contract, so the transfer out succeeds. Alex then agreed the finding is valid: the fee can be paid in a mintable token to gain temporary ownership of an underlying token, which is repaid at the end of fillOrder.

Impact

An attacker can use PuttyV2 as a flashloan pool: borrow the assets the contract holds for the duration of one transaction, use them for profit elsewhere, and return them before fillOrder completes, without paying a protocol fee in real value. The judge's framing was that flash loans from the contract would be a feature rather than a bug, but doing so without paying a protocol fee, by paying in fake tokens, would not be great.

Fix

Putty Finance acknowledged the issue as technically true and said no easy mitigation exists as far as they could see, apart from adding nonReentrant to exercise and fillOrder, which carries a non-negligible gas overhead.

  1. Look for other functions that make an external call to a caller-supplied address while holding protocol assets, and check whether a state-changing function such as exercise can be reentered from inside that call. A positive result is assets leaving the contract before the outer call settles and returning before it ends.
  2. Check whether fees are denominated in a token the caller supplies. If so, try paying the fee in a token the attacker can mint. A positive result is the fee being satisfied with no real value transferred.
  3. Check whether the protocol's reentrancy protection covers every pair of functions that touch the same balances. A positive result is a guard on one entry point but not on the other.

This kind of bug tends to hide in functions that call out to user-supplied addresses partway through settlement, especially where the protocol holds other users' assets at the same time. During review, trace every external call in the fill and settlement paths and ask what an attacker contract could do at that moment. The signal is a fee or accounting step that depends on a token or asset the caller controls. To show impact, demonstrate the full round trip in one transaction: assets out, profit taken elsewhere, assets back, and the fee paid in something worthless.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

Putty is a service that runs on a blockchain (a shared, public computer that many people use at once). Its main program, a smart contract called PuttyV2, holds assets that belong to people with open positions. An option is a contract that gives someone the right to buy or sell something at an agreed price later. Putty's main program, a smart contract called PuttyV2, holds assets that belong to people with open positions, and while it processes an order it moves those assets around.

A smart contract is a small program that lives on the blockchain. Once deployed it runs exactly as written, and anyone can call it. When one contract calls another, the second one's code runs in the middle of the first one's work. That is the heart of this finding.

The researcher found that PuttyV2 can be tricked into acting like a flashloan provider. A flashloan is a loan that must be borrowed and repaid within a single transaction (one indivisible bundle of actions on the blockchain). If the loan is not repaid by the end of that bundle, the whole bundle is cancelled as if it never happened. Flashloans are normally low risk to the lender because repayment is enforced by the transaction reverting, but lenders charge a fee for the service. Here, the attacker could borrow the assets PuttyV2 holds, use them to make money elsewhere, and give them back before the transaction ends, while paying the protocol's fee in a worthless token the attacker created themselves. So the fee is paid, but it is not worth anything.

The report was filed during a Code4rena audit of Putty in 2022. Code4rena is a platform where many independent reviewers (called wardens) examine a project's code and compete to find problems. The same issue was also found by horsefacts, pedroais and unforgiven. It was rated medium severity, meaning it is real but not the most urgent class of problem.

The pieces you need to know

Before the steps make sense, a few ideas from the report need unpacking.

  • ERC-20 is a standard for fungible tokens: units that are all identical, like a currency. A token contract keeps a table of who owns how many.
  • ERC-721 is a standard for non-fungible tokens (NFTs): each one is unique, like a numbered ticket. Putty deals in options on these.
  • baseAsset is the asset an option is denominated in. In this report it is supposed to be a normal ERC-20 token, but the attacker makes it a contract of their own with custom logic.
  • erc20Assets and erc721Assets are fields in an order that say which tokens and NFTs are involved and in what amounts.
  • fillOrder is the function that accepts and settles an order. exercise is the function that uses an option, moving the underlying assets to the holder.
  • Reentrancy is when a contract is called again while it is still in the middle of its own work. Think of a shop assistant who is halfway through counting your change when you ask them to serve you again; if the till is still open, the second request can interfere with the first. Reentrancy is a classic source of blockchain bugs.
  • A fee is a small percentage the protocol keeps for itself. Here it is 3%, charged when an option is exercised.

Setup

The attack needs no special tooling beyond the ability to deploy a contract. The preconditions described in the report are:

  1. The maker and the taker of the order are the same party, and that party is a contract. In other words, the attacker plays both sides of the trade, so there is no counterparty to object.
  2. The order is a Long Call order whose order.baseAssets points at a contract with custom logic rather than a standard ERC20. A Long Call is an option that gives the holder the right to buy the underlying asset. Because the base asset is the attacker's own contract, the attacker decides what code runs when PuttyV2 interacts with it.
  3. The order also specifies erc20Assets with a token and tokenAmount that PuttyV2 is owing, in the same way the order can specify erc721Assets. This tells the contract what it is expected to receive back at settlement.

Finding it

The researcher noticed that fillOrder calls out to order.baseAsset partway through its execution, at line 324 of PuttyV2.sol. Because baseAsset can be an arbitrary contract, that call is a point where attacker-controlled code runs while fillOrder is still in progress and while the contract's assets are still in place. From there the attacker contract can call exercise on a short call position and pull assets out of PuttyV2 before fillOrder has finished settling.

In everyday terms: the shop is halfway through a transaction, the till is open, and the customer is allowed to run a small piece of their own instructions at that exact moment. Those instructions ask the shop to hand over goods before the paperwork is complete.

Exploitation

Step 1: fill a Long Call order with a malicious base asset

The attacker contract calls PuttyV2.fillOrder with a Long Call order. The order's baseAssets reference a contract with custom logic instead of a standard ERC20. The order also sets erc20Assets to the token and tokenAmount that PuttyV2 is owing, mirroring how erc721Assets works.

Why this step: the attacker needs fillOrder to start running, because that is the function that will later call the attacker's contract. By making the base asset their own contract, the attacker plants a hook that PuttyV2 will call on its own.

Step 2: gain control during fillOrder

Execution reaches the external call at:

Text
https://github.com/code-423n4/2022-06-putty/blob/[redacted]/contracts/src/PuttyV2.sol#L324

At that point the custom logic in the attacker's contract runs, with order.baseAsset as the contract address being called.

Line by line: the address above is a link to the exact line of Putty's source code where fillOrder makes the call out. When the blockchain reaches that line, control passes to whatever contract sits at order.baseAsset. Since the attacker supplied that address, the attacker's code now runs, and it runs inside fillOrder, before fillOrder has finished. This is the reentrancy window.

Step 3: exercise the short call position

The malicious contract calls exercise to exercise the short call position. That call transfers the assets named in the order out to the malicious contract, through the logic in _transferERC20sOut and _transferERC721sOut.

Why this step: exercise is the function that pays out the option's underlying assets. Calling it from inside fillOrder means the payout happens while the outer order is still unsettled. The two helper functions named here are the internal routines that move ERC-20 tokens and NFTs out of the contract.

Step 4: use the assets elsewhere

The contract uses the borrowed assets to make a profit on other platforms. Execution then returns to the same point in fillOrder at line 324.

Why this step: this is the flashloan part. The assets are only needed for a moment. The attacker puts them to work somewhere else, earns a return, and then lets execution flow back into fillOrder. Because everything happens in one transaction, the attacker never needed to own the assets in the first place.

Step 5: repay at the end of fillOrder

At the end of fillOrder, the contract transfers enough assets back to PuttyV2 through _transferERC20sIn and _transferERC721sIn, and the execution completes.

Why this step: the loan has to be repaid before the transaction ends, or the whole thing is cancelled. These two helpers move tokens and NFTs back into the contract. Once they are back, fillOrder finishes normally and the transaction is accepted.

Root cause

fillOrder performs an external call to order.baseAsset while it is mid-flight, and exercise can be called reentrantly from inside that call. Putty Finance noted that no easy mitigation exists apart from adding nonReentrant to exercise and fillOrder, so the attacker can take assets out during the window and put them back before fillOrder finishes. A reentrancy guard is a simple lock: a flag that says "this function is already running, do not let it run again until I am done". Putty had no such lock shared between these two functions.

The fee is the second half of the problem. As pedroais put it, the 3% is paid in the fake asset, so there is no real fee to perform the attack. The assets moved out are already inside the contract, so the transfer out does not revert. To revert means to cancel and undo everything; a transfer that would normally fail because the sender lacks funds does not fail here, because the contract already holds the assets.

The wardens went back and forth on whether the sequence works at all. Alex the Entreprenerd first argued that an exercised option pays a 3% fee and that a mid-fillOrder exercise would revert for lack of tokens, because the order's ERC20s and NFTs had not yet been transferred in. Pedroais answered that the fee is paid in the fake asset and that the assets are already in the contract, so the transfer out succeeds. Alex then agreed the finding is valid: the fee can be paid in a mintable token to gain temporary ownership of an underlying token, which is repaid at the end of fillOrder. A mintable token is one whose creator can make more of it at will, which is why paying a fee in it costs the attacker nothing real.

Impact

An attacker can use PuttyV2 as a flashloan pool: borrow the assets the contract holds for the duration of one transaction, use them for profit elsewhere, and return them before fillOrder completes, without paying a protocol fee in real value. The judge's framing was that flash loans from the contract would be a feature rather than a bug, but doing so without paying a protocol fee, by paying in fake tokens, would not be great.

Fix

Putty Finance acknowledged the issue as technically true and said no easy mitigation exists as far as they could see, apart from adding nonReentrant to exercise and fillOrder, which carries a non-negligible gas overhead. nonReentrant is the standard lock mentioned above; gas is the small fee paid to run code on the blockchain, so adding the lock makes every call slightly more expensive.

  1. Look for other functions that make an external call to a caller-supplied address while holding protocol assets, and check whether a state-changing function such as exercise can be reentered from inside that call. A positive result is assets leaving the contract before the outer call settles and returning before it ends.
  2. Check whether fees are denominated in a token the caller supplies. If so, try paying the fee in a token the attacker can mint. A positive result is the fee being satisfied with no real value transferred.
  3. Check whether the protocol's reentrancy protection covers every pair of functions that touch the same balances. A positive result is a guard on one entry point but not on the other.

This kind of bug tends to hide in functions that call out to user-supplied addresses partway through settlement, especially where the protocol holds other users' assets at the same time. During review, trace every external call in the fill and settlement paths and ask what an attacker contract could do at that moment. The signal is a fee or accounting step that depends on a token or asset the caller controls. To show impact, demonstrate the full round trip in one transaction: assets out, profit taken elsewhere, assets back, and the fee paid in something worthless.

Glossary

  • Blockchain: a shared, public record of transactions that many computers maintain together.
  • Smart contract: a program deployed on a blockchain that runs exactly as written and can be called by anyone.
  • Transaction: one bundle of actions on the blockchain that either all succeed or all fail together.
  • Flashloan: a loan borrowed and repaid within a single transaction, so the lender is exposed only for that instant.
  • ERC-20: a standard for fungible tokens, where every unit is identical.
  • ERC-721: a standard for non-fungible tokens (NFTs), where each token is unique.
  • NFT: a non-fungible token, a unique digital item tracked on the blockchain.
  • Option: a contract giving the right to buy or sell an asset at an agreed price later.
  • Long Call: an option giving the holder the right to buy the underlying asset.
  • Short call position: the side that has sold the call and must deliver the asset if it is exercised.
  • Exercise: to use an option, triggering the transfer of the underlying assets.
  • fillOrder: the Putty function that accepts and settles an order.
  • baseAsset: the asset an option is denominated in.
  • erc20Assets / erc721Assets: order fields naming which tokens and NFTs are involved and in what amounts.
  • Reentrancy: calling a contract again while it is still in the middle of its own work.
  • Reentrancy guard / nonReentrant: a lock that stops a function from being re-entered until it finishes.
  • Fee: a percentage the protocol keeps for itself, here 3% on exercise.
  • Revert: to cancel a transaction and undo its effects, usually because a check failed.
  • Mintable token: a token whose creator can issue more of it at will.
  • Gas: the small fee paid to run code on the blockchain.
  • Code4rena: a platform where independent reviewers audit code and compete to find problems.
  • Warden: a reviewer taking part in a Code4rena audit.
  • Severity: the rating given to a finding, here medium.

More by 0xc0ffEE

View all

27 more disclosed reports, best first.

More from Putty

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.