Selector Collision Attack
The selector collision attack was one of the key reasons behind the hacking of the Poly Network cross-chain bridge.
In August 2021, the cross-chain bridge contracts of Poly Network on ETH, BSC, and Polygon were hacked, resulting in losses of up to $611 million. This was the largest blockchain hack of 2021 and ranked second in the history of stolen amounts, we can learn more about the detailed attack incidents from this article.
In Ethereum smart contracts, a function selector is the first 4
bytes (8
hexadecimal digits) of the hash of the function signature "<function name>(<function inputTypes>)"
. When a user calls a contract's function, the first 4
bytes of the calldata
are the target function's selector, which determines which function to call.
Due to the function selector being only 4
bytes long, it's quite short and prone to collisions: it's relatively easy to find two different functions that share the same function selector. For example, mint(address,uint256)
and cat642998653(address,uint256)
have the same selector: 0x23b872dd
.
Vulnerable Contract Example
让我们通过一个具有漏洞的合约示例来学习一下。 SelectorCollisionTest
合约有一个状态变量 isCompleted
,初始值为 false
。 攻击者需要将其改为 true
。 合约主要有 2
个函数。
-
activateKey()
:攻击者可以调用这个函数将isCompleted
改为true
,完成攻击。 然而,这个函数检查msg.sender == address(this)