Overlap Between ERC721.transferFrom() and ERC20.transferFrom() Allows order.erc20Assets or order.baseAsset To Be ERC721 Rather Than ERC20
The contract's order parameters allow an ERC721 token address to be used where an ERC20 token is expected because both token standards share the same transferFrom function signature. An attacker could fill an order with an ERC721 as the base or ERC20 asset, causing the NFT to be transferred into the contract but then permanently stuck because the outgoing transfer uses an ERC20-specific function.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by reassor and sseefried
Impact
Both ERC20.transferFrom(address to, address from, uint256 amount) and ERC721.transferFrom(address to, address from, uint256 id) have the same function signature 0x23b872dd. The impact of this is it’s possible for baseAsset or erc20Assets to be ERC721 addresses rather than ERC20.
These functions will successfully transfer the NFT into the protocol however they will fail to transfer the NFT out of the contract. That is because the outgoing transfer is ERC20.safeTransfer() which calls transfer(to, amount) which does not match up with any valid function signatures on ERC721.
Therefore any ERC721 tokens transferred into the contract in this manner via fillOrder() will be permanently stuck in the contract as neither exercise() nor withdraw() will successfully transfer the tokens out of the contract.
Proof of Concept
function transferFrom(
address from,
address to,
uint256 id
) public virtual {
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
Recommended Mitigation Steps
Consider whitelisting approved ERC721 and ERC20 token contracts. Furthermore, separate these two contracts into different whitelists for ERC20s and ERC721s then ensure each contract is in the right category.
outdoteth (Putty Finance) acknowledged and commented:
Report: If an ERC721 token is used in places where ERC20 assets are supposed to be used then ERC721 tokens can get stuck in withdraw() and exercise().
hyh (warden) reviewed mitigation:
Requires asset whitelisting, now addressed on UI/DB level.
Context
Putty is a protocol for peer-to-peer options on NFTs. The finding concerns PuttyV2.sol, the main contract of the protocol, and specifically the order-filling path: fillOrder() takes an order that names a baseAsset and a list of erc20Assets, and moves those assets between the parties. The contract treats those asset addresses as ERC20 tokens: incoming transfers use transferFrom, and outgoing transfers use ERC20.safeTransfer(), which in turn calls transfer(to, amount).
The report was submitted by kirk-baird, and reassor and sseefried independently found the same issue. It was rated medium severity.
Setup
The report does not describe any accounts, roles, tooling or deployed environment. The analysis is a source-level review of the contracts in the Code4rena repository for the 2022-06 Putty audit. The relevant files are contracts/src/PuttyV2.sol and the Solmate token implementations under contracts/lib/solmate/src/tokens/. The report links the following lines of PuttyV2.sol as the affected code: 324, 338, 344, 360, 436 and 601.
The precondition is simply that an order can be created or filled in which baseAsset or one of the erc20Assets is the address of an ERC721 contract rather than an ERC20 contract.
Finding it
The researcher noticed that the two token standards share a function selector. Both of these functions hash to the same four-byte signature, 0x23b872dd:
ERC20.transferFrom(address to, address from, uint256 amount)ERC721.transferFrom(address to, address from, uint256 id)
Because the selector is identical, a call that the protocol intends as an ERC20 transfer will be dispatched successfully by an ERC721 contract, and vice versa. Nothing in the call itself distinguishes the two standards. The consequence the researcher draws is that baseAsset or erc20Assets can be ERC721 addresses rather than ERC20 addresses, and the incoming leg of the transfer will still succeed.
The asymmetry is on the way out. The outgoing transfer is ERC20.safeTransfer(), which calls transfer(to, amount). That signature does not match any valid function on ERC721, so the outgoing leg fails. Tokens that came in through fillOrder() therefore cannot leave through exercise() or withdraw().
Exploitation
Step 1: Confirm the selector collision
The proof of concept is the pair of Solmate implementations. The ERC721 version:
function transferFrom(
address from,
address to,
uint256 id
) public virtual {
The ERC20 version:
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
Both take (address, address, uint256), so both are reachable through the selector 0x23b872dd. The parameter names differ, and the ERC20 version returns a bool while the ERC721 version does not, but neither difference affects selector matching.
Step 2: Get an ERC721 into the contract through fillOrder()
An order is filled in which baseAsset or an entry of erc20Assets is an ERC721 contract address. The incoming transfer is issued as transferFrom with the shared selector, so the ERC721 contract accepts it and the NFT is moved into the protocol. The report states this transfer succeeds.
Step 3: Observe that the NFT cannot leave
When the protocol tries to send the asset back out, it uses ERC20.safeTransfer(), which calls transfer(to, amount). That does not match any valid function signature on ERC721, so the call fails. The report states that neither exercise() nor withdraw() will successfully transfer the tokens out of the contract.
Result
Any ERC721 token transferred into the contract in this manner via fillOrder() is permanently stuck in the contract.
Root cause
The protocol identifies assets by address and assumes the standard behind that address. It never checks whether the address implements ERC20 or ERC721. The incoming path happens to work for both standards because transferFrom(address, address, uint256) has the same selector in ERC20 and ERC721, so the collision masks the missing check. The outgoing path uses a different function, transfer(to, amount), which exists only on ERC20, so the mismatch surfaces only on the way out. The report does not describe any existing whitelist or type check that would have prevented an ERC721 address from being used where an ERC20 is expected.
Impact
If an ERC721 token is used where an ERC20 asset is expected, the NFT is accepted into the contract by fillOrder() and then cannot be moved out by exercise() or withdraw(). The token is permanently stuck. The report frames this as affecting baseAsset or erc20Assets and does not describe any path by which an attacker profits beyond the loss of the trapped token.
Fix
The recommended mitigation is to whitelist approved ERC721 and ERC20 token contracts, and to keep two separate whitelists, one for ERC20s and one for ERC721s, ensuring each contract is placed in the correct category.
Putty acknowledged the report, summarising it as: if an ERC721 token is used in places where ERC20 assets are supposed to be used, then ERC721 tokens can get stuck in withdraw() and exercise(). The warden review of the mitigation notes that it requires asset whitelisting, now addressed at the UI/DB level.
Variants to test
- Other shared selectors between standards. Look for functions whose four-byte selector exists on both the expected and the unexpected token standard, and where the protocol relies on the call reverting for the wrong type. A positive result is an asset of the wrong standard being accepted on the way in.
- Asymmetric in/out function pairs. Where a protocol uses one function to pull assets and a different one to push them, check whether the pull function is standard-agnostic while the push function is not. A positive result is an asset that enters but cannot exit.
- Address-typed asset fields without type checks. Any order or listing structure with a free-form token address and no whitelist is a candidate. A positive result is an order that fills successfully with a contract of the wrong standard.
- Return value handling. The ERC20
transferFromreturnsbooland the ERC721 one does not; check whether the protocol's decoding of the return value behaves differently between the two, and whether that produces a second failure mode.
Hunting notes
This class of bug hides in code that stores token addresses as plain address values and dispatches calls by selector without validating the interface. During review, the signal is a mix of transferFrom on the way in and safeTransfer or transfer on the way out, with no whitelist or supportsInterface check in between. The project acknowledged the stuck-token outcome, and the fix direction, separate whitelists per standard, follows directly from the root cause. The acknowledgement from the project confirms the stuck-token outcome, and the fix direction, separate whitelists per standard, follows directly from the root cause.
What happened, in plain words
Putty is a service that lets two people make a deal about an NFT (a unique digital collectible) without going through a central marketplace. One person offers something, the other person offers something else, and a piece of software called a smart contract holds the deal together. A smart contract is a program that lives on a blockchain and runs automatically when certain conditions are met, a bit like a vending machine: you put something in, it does exactly what its code says, and nobody can change its mind afterwards.
The bug in this report is about the moment the contract moves things between the two people. The contract was written assuming that everything being moved is a "fungible token" (a token where every unit is identical, like a digital coin), but it never actually checked that. Because of a quirk in how the two most common token standards name their functions, an NFT could be accepted into the contract as if it were a coin, and then it could never be sent back out. The NFT would be stuck in the contract forever.
This was rated a medium severity issue. It was found by kirk-baird, and two other researchers, reassor and sseefried, found the same problem independently.
Background you need first
Before the details make sense, here are the ideas the report relies on.
Tokens and token standards
On a blockchain like Ethereum, a "token" is not a separate coin with its own chain. It is a smart contract that keeps a list of who owns how much. Two standards describe how such contracts should behave so that wallets and other software know how to talk to them:
- ERC20 is the standard for fungible tokens. Think of it like a currency: one unit is the same as any other unit. The standard defines functions such as
transfer(send some units to someone) andtransferFrom(send some units on someone's behalf, usually after they have given permission). - ERC721 is the standard for non-fungible tokens, NFTs. Each token has a unique ID number, and there is exactly one of each. Think of it like a numbered ticket or a deed to a specific house: it is not interchangeable with another one. The standard also defines a function called
transferFrom, but here the third argument is the ID of the specific NFT, not an amount.
Function selectors
When one contract calls a function on another contract, it does not send the function's name as text. It sends a short numeric code, four bytes long, called the selector. The selector is calculated from the function's name and the types of its arguments, for example transferFrom(address,address,uint256). Two functions with the same name and the same argument types get the same selector, even if the argument names differ or the functions belong to different standards. This is the heart of the bug.
What fillOrder() does
Putty's main contract is called PuttyV2.sol. The function at the centre of this report is fillOrder(). It takes an order, which is a description of a deal, and carries it out. The order names two kinds of assets:
- a
baseAsset, the main thing being traded, and - a list of
erc20Assets, additional tokens included in the deal.
The contract moves these assets between the two parties. When assets come in, it uses transferFrom. When assets go out, it uses ERC20.safeTransfer(), which in turn calls transfer(to, amount). Both of those are ERC20 functions. The contract never asks whether the address it was given is really an ERC20 contract.
The affected code
The report points at six specific lines of PuttyV2.sol as the affected code: lines 324, 338, 344, 360, 436 and 601. It does not quote those lines, so the exact code at each line is not reproduced here.
Setup
The report does not describe any accounts, roles, tools or a deployed environment. This was a source-level review: the researcher read the code of the contracts in the Code4rena repository for the June 2022 Putty audit and reasoned about what it would do. The relevant files are contracts/src/PuttyV2.sol and the token implementations under contracts/lib/solmate/src/tokens/. Solmate is a library of ready-made token contracts that Putty uses.
The only precondition is that an order can be created or filled in which baseAsset, or one of the erc20Assets, is the address of an ERC721 contract instead of an ERC20 contract. In other words, someone has to be able to put an NFT's contract address into a field that was meant for a coin's contract address.
Finding it
The researcher noticed that the two standards share a function selector. Both of these functions produce the same four-byte signature, 0x23b872dd:
ERC20.transferFrom(address to, address from, uint256 amount)ERC721.transferFrom(address to, address from, uint256 id)
Both take two addresses and one number, so the selector, which is built from the name and the argument types, comes out identical. The names of the arguments (amount versus id) and the fact that the ERC20 version returns a true/false value while the ERC721 version does not, make no difference to the selector.
Because the selector is identical, a call that the protocol intends as an ERC20 transfer will be accepted by an ERC721 contract, and the other way round. Nothing in the call itself says which standard is meant. The consequence the researcher draws is that baseAsset or erc20Assets can be ERC721 addresses rather than ERC20 addresses, and the incoming half of the transfer will still succeed.
The problem appears on the way out. The outgoing transfer is ERC20.safeTransfer(), which calls transfer(to, amount). That function name and argument list do not match any valid function on ERC721, so the outgoing half fails. Tokens that came in through fillOrder() therefore cannot leave through exercise() or withdraw(). exercise() is the step where an option is used, and withdraw() is the step where assets are taken out of the contract.
Exploitation
Step 1: Confirm the selector collision
The proof of concept is the pair of Solmate implementations. The ERC721 version:
function transferFrom(
address from,
address to,
uint256 id
) public virtual {
Line by line:
function transferFrom(starts the declaration of a function namedtransferFrom.address from,is the first argument: the address the token is being sent from.address to,is the second argument: the address the token is being sent to.uint256 idis the third argument: the unique number of the NFT being moved.) public virtual {means anyone can call it, and it can be overridden by a contract that inherits from this one. The body of the function follows, but the report only quotes the signature.
The ERC20 version:
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
Line by line:
function transferFrom(starts a function with the same name.address from,andaddress to,are the same two address arguments as before.uint256 amountis the third argument, but here it means how many units of the token to move, not which NFT.) public virtual returns (bool) {means anyone can call it, it can be overridden, and it reports back a true or false value to say whether it worked.
Both take (address, address, uint256), so both are reachable through the selector 0x23b872dd. The argument names differ, and the ERC20 version returns a bool while the ERC721 version does not, but neither difference affects selector matching. This is why the researcher could be confident that the incoming call would be accepted by an NFT contract.
Step 2: Get an ERC721 into the contract through fillOrder()
An order is filled in which baseAsset or an entry of erc20Assets is an ERC721 contract address. The incoming transfer is issued as transferFrom with the shared selector, so the ERC721 contract accepts it and the NFT is moved into the protocol. The report states that this transfer succeeds. The reason this step matters is that it shows the mistake is silent: nothing errors, nothing warns, and the contract believes it has received a coin when it has actually received an NFT.
Step 3: Observe that the NFT cannot leave
When the protocol tries to send the asset back out, it uses ERC20.safeTransfer(), which calls transfer(to, amount). That does not match any valid function signature on ERC721, so the call fails. The report states that neither exercise() nor withdraw() will successfully transfer the tokens out of the contract. This step matters because it turns a harmless-looking mix-up into a permanent loss: the asset went in through a normal path and no exit path can move it.
Result
Any ERC721 token transferred into the contract in this manner via fillOrder() is permanently stuck in the contract.
Root cause
The protocol identifies assets by address and assumes the standard behind that address. It never checks whether the address implements ERC20 or ERC721. The incoming path happens to work for both standards because transferFrom(address, address, uint256) has the same selector in ERC20 and ERC721, so the collision hides the missing check. The outgoing path uses a different function, transfer(to, amount), which exists only on ERC20, so the mismatch only shows up on the way out. The report does not describe any existing whitelist or type check that would have prevented an ERC721 address from being used where an ERC20 is expected. A whitelist is a list of approved addresses; anything not on the list is refused.
Impact
If an ERC721 token is used where an ERC20 asset is expected, the NFT is accepted into the contract by fillOrder() and then cannot be moved out by exercise() or withdraw(). The token is permanently stuck. The report frames this as affecting baseAsset or erc20Assets and does not describe any path by which an attacker profits beyond the loss of the trapped token.
Fix
The recommended mitigation is to whitelist approved ERC721 and ERC20 token contracts, and to keep two separate whitelists, one for ERC20s and one for ERC721s, ensuring each contract is placed in the correct category. In everyday terms: keep two guest lists, one for coins and one for collectibles, and check which list an address is on before accepting it.
Putty acknowledged the report, summarising it as: if an ERC721 token is used in places where ERC20 assets are supposed to be used, then ERC721 tokens can get stuck in withdraw() and exercise(). The warden review of the mitigation notes that it requires asset whitelisting, now addressed at the UI/DB level. In other words, the warden notes the whitelisting is addressed at the UI/DB level.
Variants to test
These are directions for looking for similar bugs elsewhere, written for someone learning to review code.
- Other shared selectors between standards. Look for functions whose four-byte selector exists on both the expected and the unexpected token standard, and where the protocol relies on the call failing for the wrong type. A positive result is an asset of the wrong standard being accepted on the way in.
- Asymmetric in/out function pairs. Where a protocol uses one function to pull assets and a different one to push them, check whether the pull function works for any standard while the push function does not. A positive result is an asset that enters but cannot exit.
- Address-typed asset fields without type checks. Any order or listing structure with a free-form token address and no whitelist is a candidate. A positive result is an order that fills successfully with a contract of the wrong standard.
- Return value handling. The ERC20
transferFromreturnsbooland the ERC721 one does not; check whether the protocol's decoding of the return value behaves differently between the two, and whether that produces a second failure mode.
Hunting notes
This class of bug hides in code that stores token addresses as plain address values and dispatches calls by selector without validating the interface. During review, the signal is a mix of transferFrom on the way in and safeTransfer or transfer on the way out, with no whitelist or supportsInterface check in between. supportsInterface is a standard way for a contract to say which interfaces it implements, so a check against it would have caught the mismatch. The project acknowledged the stuck-token outcome, and the fix direction, separate whitelists per standard, follows directly from the root cause. The acknowledgement from the project confirms the stuck-token outcome, and the fix direction, separate whitelists per standard, follows directly from the root cause.
Glossary
- Smart contract: a program stored on a blockchain that runs automatically and cannot be changed after deployment.
- Blockchain: a shared, append-only record of transactions maintained by many computers.
- NFT: a non-fungible token, a unique digital item identified by a number, covered by the ERC721 standard.
- ERC20: the standard for fungible tokens, where every unit is interchangeable, like a currency.
- ERC721: the standard for non-fungible tokens, where each token has a unique ID.
- Fungible: interchangeable; one unit is as good as another.
- Non-fungible: unique; not interchangeable with another item.
- Token: a smart contract that records who owns how much, or which unique items.
- Function: a named block of code inside a contract that can be called to do something.
- Function selector: a four-byte code derived from a function's name and argument types, used to say which function a call is for.
- Signature (function): the name plus argument types of a function, for example
transferFrom(address,address,uint256). transferFrom: a function that moves tokens from one address to another, often on someone's behalf.transfer: a function that sends tokens to an address.safeTransfer: a wrapper that callstransferand checks the result.fillOrder(): the Putty function that carries out an order and moves the named assets between the parties.exercise(): the Putty function that uses an option.withdraw(): the Putty function that takes assets out of the contract.baseAsset: the main asset named in a Putty order.erc20Assets: the list of additional ERC20 tokens named in a Putty order.PuttyV2.sol: the main smart contract file of the Putty protocol.- Solmate: a library of ready-made token contracts used by Putty.
- Whitelist: a list of approved addresses; anything not on it is refused.
supportsInterface: a standard function through which a contract reports which interfaces it implements.- UI: user interface, the screens a person uses to interact with the service.
- DB: database, the storage where a service keeps its records.
- Warden: in a Code4rena audit, a security researcher who reviews the code and submits findings.
- Code4rena: a platform that runs public audits where researchers review code for rewards.
- Severity: a rating of how serious a finding is, here medium.
- Proof of concept: a small demonstration that shows a bug is real.
- Root cause: the underlying reason a bug exists, as opposed to its visible symptom.
- Mitigation: a change that removes or reduces a problem.
- Selector collision: when two different functions end up with the same four-byte selector.