Merkle leaves are the same length as the parents that are hashed
The Merkle tree implementation hashes leaves and parent nodes to the same 64-byte length, allowing a hash collision between a leaf and an internal node. An attacker could craft a fraudulent proof to claim airdrop tokens for an invalid address, disrupting the airdrop.
Originally disclosed on Code4rena
Submitted by AuditsAreUS
Impact
The size of a leaf is the same size of the parent data that is hashed, both are 64 bytes. As a result it is possible to have a hash collision between a leaf and any node in the tree. This allows for proofs to be repeated multiple times by taking subtrees as leaves.
Fraudulent proofs will disrupt the airdrop and transfer funds to invalid addresses.
Proof of Concept
For example consider the following binary tree which has 4 leaves d,e,f,g
a->b a->c
b->d b->e
c->f c->g
To calculate the parent hash for c it is keccak(f || g) and the parent hash for a is keccak(b || c) as seen in the parentHash() function below.
function parentHash(bytes32 a, bytes32 b) public pure returns (bytes32) {
if (a < b) {
return keccak256(abi.encode(a, b));
} else {
return keccak256(abi.encode(b, a));
}
}
A leaf is calculated as
bytes32 leaf = keccak256(abi.encode(destination, value));
abi.encode(address,uint) will output 64 bytes. Since abi.encode(bytes32,bytes32) will also be 64 bytes it is possible to have a hash collision between a leaf and a parent node.
Taking the example above if we now set destination = keccak(e || d) and value = keccak(f || g) and provide the proof as an empty array since we are already at a, the root []. This proof will verify for destination and value set to the hash of each child node.
This issue is rated as medium as there are some drawbacks to the attack that will make it challenging to pull off in practice. The first is that destination is a 20 bytes address and thus will require the node in the tree to have 12 leading zero bytes which may not occur. Second is the value is transferred to the user and so it is likely that the balance of the contract will not be sufficient for this transfer to succeed.
Recommended Mitigation Steps
Consider using leaf = keccak(abi.encodePacked(destination, value)) in withdraw() as this will reduce the size of the leaf data being hashed to 52 bytes.
Since keccak256 prevents length extension attacks a different length of data to be hashed can be assumed to give different hashes and prevent a collision between a leaf and other nodes in the tree.
illuzen (FactoryDAO) acknowledged and commented:
Valid. Interesting attack, but even if it is successful, it will transfer tokens to what is almost certainly an unusable address, making this both very unlikely (due to issues you mentioned) and of zero benefit to the attacker (griefing). 12 sequential zero bytes would occur approximately 1 out of every 16^12 = 281474976710656 nodes.
I recommend bonus points for this one.
Justin Goro (judge) commented:
Very impressive analysis.
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 leaf hash and parent hash both use abi.encode with two 32-byte arguments, producing 64-byte inputs, so a leaf can collide with a parent node.
Impact
Fraudulent proofs could disrupt the airdrop and transfer funds to invalid addresses, though the attack is challenging because the destination must have 12 leading zero bytes and the contract balance may be insufficient.
What the attacker needed
Knowledge of Merkle tree structure; ability to submit a proof to the contract; a node with 12 leading zero bytes (unlikely)
The fix
The leaf should be computed with abi.encodePacked to reduce its size to 52 bytes, preventing collisions with parent nodes.
How it was found
- Set destination to the hash of two child nodes (e.g., keccak(e || d)).
- Set value to the hash of two other child nodes (e.g., keccak(f || g)).
- Provide an empty proof array since the computed leaf equals the root.
- The proof verifies for the crafted destination and value.
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 Merkle tree is a way to prove that a piece of data is part of a larger set without revealing the whole set. Each leaf is hashed, and pairs of hashes are combined to form parent hashes. If a leaf and a parent hash have the same length, an attacker might trick the system into accepting a fake proof by making a leaf look like a parent. This could let them claim tokens for an address that doesn't exist.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.