The preimage DB (i.e., NameWrapper.names ) can be maliciously manipulated/corrupted
The NameWrapper contract's preimage database can be corrupted because it checks whether a node is wrapped by comparing the ENS registry owner to the contract address, which an attacker can spoof by directly setting the owner via the ENS registry. An attacker could then wrap a subdomain without recording its name, leading to incorrect or empty entries in the preimage database.
Originally disclosed on Code4rena
Submitted by PwnedNoMore
By design, the NameWrapper.names is used as a preimage DB so that the client can query the domain name by providing the token ID. The name should be correctly stored. To do so, the NameWrapper record the domain’s name every time it gets wrapped. And as long as all the parent nodes are recorded in the DB, wrapping a child node will be very efficient by simply querying the parent node’s name.
However, within a malicious scenario, it is possible that a subdomain can be wrapped without recording its info in the preimage DB.
Specifically, when NameWrappper.setSubnodeOwner / NameWrappper.setSubnodeRecord on a given subdomain, the following code is used to check whether the subdomain is wrapped or not. The preimage DB is only updated when the subdomain is not wrapped (to save gas I beieve).
function setSubnodeOwner(
bytes32 parentNode,
string calldata label,
address newOwner,
uint32 fuses,
uint64 expiry
)
public
onlyTokenOwner(parentNode)
canCallSetSubnodeOwner(parentNode, keccak256(bytes(label)))
returns (bytes32 node)
{
bytes32 labelhash = keccak256(bytes(label));
node = _makeNode(parentNode, labelhash);
(, , expiry) = _getDataAndNormaliseExpiry(parentNode, node, expiry);
if (ens.owner(node) != address(this)) {
ens.setSubnodeOwner(parentNode, labelhash, address(this));
_addLabelAndWrap(parentNode, node, label, newOwner, fuses, expiry);
} else {
_transferAndBurnFuses(node, newOwner, fuses, expiry);
}
}
However, the problem is that ens.owner(node) != address(this) is not sufficient to check whether the node is alreay wrapped. The hacker can manipulate this check by simply invoking EnsRegistry.setSubnodeOwner to set the owner as the NameWrapper contract without wrapping the node.
Consider the following attack scenario.
-
the hacker registers a 2LD domain, e.g.,
base.eth -
he assigns a subdomain for himself, e.g.,
sub1.base.eth- the expiry of
sub1.base.ethshould be set as expired shortly - note that the expiry is for
sub1.base.ethinstead ofbase.eth, so it is safe to make it soonly expired
- the expiry of
-
the hacker waits for expiration and unwraps his
sub1.base.eth -
the hacker invokes
ens.setSubnodeOwnerto set the owner ofsub2.sub1.base.ethas NameWrapper contract -
the hacker re-wraps his
sub1.base.eth -
the hacker invokes
nameWrapper.setSubnodeOwnerforsub2.sub1.base.eth- as such,
names[namehash(sub2.sub1.base.eth)]becomes empty
- as such,
-
the hacker invokes
nameWrapper.setSubnodeOwnerforeth.sub2.sub1.base.eth.- as such,
names[namehash(eth.sub2.sub1.base.eth)]becomes\x03eth
- as such,
It is not rated as a High issue since the forged name is not valid, i.e., without the tailed \x00 (note that a valid name should be like \x03eth\x00). However, the preimage BD can still be corrupted due to this issue.
Suggested Fix
When wrapping node X, check whether NameWrapper.names[X] is empty directly, and update the preimage DB if it is empty.
Proof of Concept / Attack Scenario
For full details, please see original warden submission.
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 check `ens.owner(node) != address(this)` is used to determine if a node is already wrapped, but this can be true even when the node was not wrapped through NameWrapper, allowing the preimage database to be bypassed.
Impact
The preimage database can be corrupted, leading to incorrect or empty name records, though the forged name is not valid and the issue is not rated as High.
What the attacker needed
Ability to register a 2LD domain; ability to set subdomain expiry; wait for expiry and unwrap; call ENS registry and NameWrapper functions
The fix
When wrapping a node, check whether NameWrapper.names[node] is empty directly and update the preimage database if it is empty.
How it was found
- Register a 2LD domain, e.g., base.eth.
- Assign a subdomain sub1.base.eth with an expiry set to expire shortly.
- Wait for expiration and unwrap sub1.base.eth.
- Invoke ens.setSubnodeOwner to set the owner of sub2.sub1.base.eth as the NameWrapper contract.
- Re-wrap sub1.base.eth.
- Invoke nameWrapper.setSubnodeOwner for sub2.sub1.base.eth, causing names[namehash(sub2.sub1.base.eth)] to become empty.
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.
The NameWrapper contract keeps a database that maps token IDs to domain names, so clients can look up names. It decides whether to add a name to this database by checking if the ENS registry says the contract owns the node. An attacker can make the registry say the contract owns a node without actually wrapping it, so the database can end up with missing or wrong names.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.