`abi.encodePacked` Allows Hash Collision
The Factory contract hashed several user-supplied dynamic arguments together with abi.encodePacked before checking a signature, so different argument combinations could produce the same hash. An attacker could therefore pass values that were never signed and still satisfy the signedOnly modifier.
keccak123 Follow with 0xheynacho
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-10-nftport-judging/issues/118
Found by
keccak123, 0xheynacho
Summary
From the solidity documentation:
https://docs.soliditylang.org/en/v0.8.17/abi-spec.html?highlight=collisions#non-standard-packed-mode
> If you use keccak256(abi.encodePacked(a, b)) and both a and b are dynamic types, it is easy to craft collisions in the hash value by moving parts of a into b and vice-versa. More specifically, abi.encodePacked("a", "bc") == abi.encodePacked("ab", "c").
This issue exists in the Factory contract can results in hash collisions, bypassing the signedOnly modifier.
Vulnerability Detail
The issue is in these lines of code: https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L171 https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L195 https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L222
As the solidity docs describe, two or more dynamic types are passed to abi.encodePacked. Moreover, these dynamic values are user-specified function arguments in external functions, meaning anyone can directly specify the value of these arguments when calling the function. The signedOnly modifier is supposed to protect functions to permit only function arguments that have been properly signed to be passed to the function logic, but because a collision can be created, the modifier can be bypassed for certain select inputs that result in the same encodePacked value.
Impact
The signedOnly modifier (line 537) is not effective because the modifier can be bypassed by different function arguments that result in the same signature when the values are encodePacked together. This can result in the submission of values that were not actually signed.
Code Snippet
All instances of abi.encodePacked in the contract pass multiple dynamic type arguments
https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L171
https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L195
https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/Factory.sol#L222
Tool used
Manual Review
Recommendation
Instead of writing functions to accept several arguments that are hashed inside the function, consider rewriting the function to take the hashed value as a function argument directly so that the hashing process happens off-chain. This approach would solve the issue and save gas.
Discussion
Evert0x
Downgrading to medium severity, fails to show an exploit pattern.
hyperspacebunny
Fixed in https://github.com/nftport/evm-minting-sherlock-fixes/pull/18
rayn731
Fixed, and it follows EIP-712 standard for hashing and signing data.
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 contract concatenated multiple dynamic, user-controlled arguments with abi.encodePacked before hashing, so the argument boundaries were ambiguous and collisions were possible.
Impact
The signedOnly check could be bypassed, allowing values that were not actually signed to reach the function logic; the judge downgraded it to medium because no concrete exploit pattern was shown.
What the attacker needed
Ability to call the external functions; craft colliding argument values
The fix
It was fixed by switching to EIP-712 standard hashing and signing of the data.
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 hash is a short fingerprint of some data. When you glue several pieces of text together before hashing, the boundaries between them disappear, so two different splits can give the same fingerprint. Here the contract trusted that fingerprint as proof of a signature, so someone could slip in data that was never actually approved.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.