QUADRIGA INITIATIVE
CRYPTO WATCHDOG & FRAUD RECOVERY PLATFORM
A COMMUNITY-BASED, NOT-FOR-PROFIT
$0 USD
JULY 2019
GLOBAL
0X PROJECT
DESCRIPTION OF EVENTS
The "0x API is a professional grade liquidity aggregator, enabling the future of DeFi applications." Their "smart order routing splits up your transaction across decentralized exchange networks to be filled with the lowest slippage possible."
"From July 23, 2018 to September 7, 2018 ConsenSys Diligence conducted a security audit of the 0x project's protocol version v2 of their contract system."
"On Friday July 12th, 0x shut down their v2 Exchange because a flaw in the signature verification routine meant that a signature of 0x04 was treated as a valid signature for all non-smart-contract accounts."
"Under normal circumstances (a conformant IFetcher implementation), this code works fine, but something surprising happens if you pass in an externally owned account (EOA). An EOA address has no associated code, and this means all calls to it succeed and return nothing. The low-level STATICCALL opcode used here will return success but will only copy as much data into memory as was returned. So even though we asked for 32 bytes of return data, nothing gets copied to memory because there is no data to copy."
"Because nothing gets copied, memory is just as it was before the call. That means ptr is still pointing to the selector data, so this function returns 0xa95c372d000000… (the function selector for fetch())."
"The same thing happens if you call a contract that implements fetch() without a return value or just implements a fallback function. The bug occurs any time the call succeeds but doesn’t return as much data as the caller expects."
"This bug manifested itself in the 0x v2.0 Exchange in a particularly nasty way due to where the bug was. The exchange allows trades to be constructed off chain and then validated and filled on chain. To know that a trade is authorized, the exchange must validate a trader’s signature."
"One option the exchange offers for validating a trader’s signature is to delegate that checking to a contract. Essentially, the trader can be a smart wallet that implements a function with the signature isValidSignature(bytes32,bytes) and returns a boolean true if the signature is valid."
"[T]he problem is that isValidSignature is called via code that’s very similar to the above vulnerable contract. If the target is not a smart contract (or otherwise returns no data), the data already in memory stays unchanged. Any non-zero value there is interpreted as true and authorizes the trade."
"Even this would be okay if traders had to specifically opt in to this type of signature scheme, but remember that these trades are constructed off chain to avoid the expense and delay of making an on chain transaction. So the person taking the proposed trade is able to dictate which type of signature checking should happen via a flag."
"All an attacker needs to do is trade with an externally owned account and specify that the signature should be checked by calling isValidSignature. This validation will succeed because no data is returned and the data already present in memory is non-zero."
The project "patched and re-deployed the[ir] entire 0x smart contract pipeline from scratch, updated [their] developer tools and packages, 0x Instant, and 0x Launch Kit." "[T]he vulnerability found in the 0x v2.0 Exchange contract was not exploited and no user funds [were] lost."
"Teams will need to point to the patched and newly deployed Exchange and AssetProxy contracts as well as clear their orderbooks of outstanding orders. Users will need to reset their allowances for the new 0x AssetProxy contracts."
The 0x project offered a "liquidity aggregator" which is used as part of an online exchange. In order to use the exchange, a trader needs to sign the transaction. However, there was a way to bypass this mechanism, which could have allowed an attacker to conduct fraudulent trades.
The exploit was fixed before any funds were taken.
HOW COULD THIS HAVE BEEN PREVENTED?
There was no loss to users of the exchange in this case.
List of Ethereum Smart Contracts Post-Mortems - Security - OpenZeppelin Community (Jun 23)
Shut down of 0x Exchange v2.0 contract and migration to patched version | by Will Warren | 0x Blog (Jun 23)
Post Mortem 0x V2 0 Exchange Vulnerability (Jun 23)
The 0x vulnerability, explained (Jun 23)
Return Data Length Validation A Bug We Missed (Jun 23)
0x: Powering the decentralized exchange of tokens on Ethereum (Jun 24)
GitHub - ConsenSys/0x_audit_report_2018-07-23: 0x Protocol v2 Audit (Jun 24)