It is possible to create fake ERC1155 NameWrapper token for subdomain, which is not owned by NameWrapper
A re-entrancy flaw in NameWrapper's transfer-and-burn-fuses routine lets a contract callback unwrap a subdomain before the new owner and fuses are written, leaving a fake ERC1155 NameWrapper token for a domain the wrapper no longer owns. An attacker could sell that token and later re-wrap the domain to steal it back.
Submitted by panprog, also found by Aussie_Battlers, brgltd, cryptphi, peritoflores, and wastewa
NameWrapper.sol#L820-L821 NameWrapper.sol#L524 NameWrapper.sol#L572
Due to re-entrancy possibility in NameWrapper._transferAndBurnFuses (called from setSubnodeOwner and setSubnodeRecord), it is possible to do some stuff in onERC1155Received right after transfer but before new owner and new fuses are set. This makes it possible, for example, to unwrap the subdomain, but owner and fuses will still be set even for unwrapped domain, creating fake ERC1155 NameWrapper token for domain, which is not owned by NameWrapper.
Fake token creation scenario:
Account1registers and wrapstest.ethdomainAccount1callsNameWrapper.setSubnodeOwnerforsub.test.ethsubdomain withAccount1as owner (to make NameWrapper owner of subdomain)Contract1smart contract is created, which calls unwrap in itsonERC1155Receivedfunction, and a function to sendsub.test.ethERC1155 NameWrapper token back toAccount1Account1callsNameWrapper.setSubnodeOwnerforsub.test.ethwithContract1as new owner, which unwraps domain back toAccount1but due to re-entrancy, NameWrapper sets fuses and ownership toContract1Account1calls function to send ERC1155 token fromContract1back to self.
After this sequence of events, sub.test.eth subdomain is owned by Account1 both in ENS registry and in NameWrapper (with fuses and expiry correctly set to the future date). Lots (but not all) of functions in NameWrapper will fail to execute for this subdomain, because they expect NameWrapper to have ownership of the domain in ENS, but some functions will still work, making it possible to make the impression of good domain.
At this point, ownership in NameWrapper is “detached” from ownership in ENS and Account1 can do all kinds of malcious stuff with its ERC1155 token. For example:
- Sell subdomain to the other user, transfering
ERC1155to that user and burningPARENT_CANNOT_CONTROLto create impression that he can’t control the domain. After receiving the payment,Account1can wrap the domain again, which burns existing ownership record and replaces with the new one with clear fuses andAccount1ownership, effectively stealing domain back from unsuspecting user, who thought thatERC1155gives him the right to the domain (and didn’t expect that parent can clear fuses whenPARENT_CANNOT_CONTROLis set). - Transfer subdomain to some other smart contract, which implements
onERC1155Received, then take it back, fooling smart contract into believing that it has received the domain.
Proof of Concept
Copy these to test/wrapper and run: yarn test test/wrapper/NameWrapperReentrancy.js
https://gist.github.com/panprog/3cd94e3fbb0c52410a4c6609e55b863e
Recommended Mitigation Steps
Consider adding nonReentrant modifiers with ReentrancyGuard implementation from openzeppelin. Alternatively just fix this individual re-entrancy issue. There are multiple ways to fix it depending on expected behaviour, for example saving ERC1155 data and requiring it to match the data after transfer (restricting onERC1155Received to not change any data for the token received):
function _transferAndBurnFuses(
bytes32 node,
address newOwner,
uint32 fuses,
uint64 expiry
) internal {
(address owner, uint32 saveFuses, uint64 saveExpiry) = getData(uint256(node));
_transfer(owner, newOwner, uint256(node), 1, "");
uint32 curFuses;
uint64 curExpiry;
(owner, curFuses, curExpiry) = getData(uint256(node));
require(owner == newOwner && saveFuses == curFuses && saveExpiry == curExpiry);
_setFuses(node, newOwner, fuses, expiry);
}
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
Likely the transfer helper calls the recipient's onERC1155Received callback before writing the new owner and fuses, so the callback can change the wrapper's state and leave it inconsistent with the ENS registry.
Impact
The attacker could sell the fake token and later re-wrap the domain to steal it back, or fool a contract into believing it received the domain; many NameWrapper functions would fail for the detached subdomain.
What the attacker needed
Attacker controls a wrapped parent domain; attacker can deploy a contract with a callback; victim buys or trusts the fake token
The fix
The report recommends adding nonReentrant modifiers from OpenZeppelin's ReentrancyGuard, or checking that the token's owner and fuses are unchanged after the transfer callback before setting new fuses.
How it was found
- Register and wrap a parent domain.
- Call setSubnodeOwner to create a subdomain owned by the attacker.
- Deploy a contract whose onERC1155Received unwraps the subdomain and can send the token back.
- Call setSubnodeOwner with that contract as new owner, triggering the unwrap during the callback.
- Call the contract to send the ERC1155 token back to the attacker.
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 can be told to run code when it receives a token, and here that code runs before the contract finishes updating who owns the token. The attacker uses that gap to unwrap the domain while the wrapper still records the attacker's contract as owner, so the token looks real but the wrapper no longer controls the domain. The attacker can then sell that misleading token and later take the domain back.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.