Smart contract bugs / Can the platform be trusted?
Compiler bugs
The source code is correct, but the compiler produces wrong bytecode. In 2023, a bug in some Vyper versions broke the reentrancy lock of several Curve pools.
1 real exploit · $69.3M lost
How the attack works
- A compiler version has a bug: a lock that does not lock, a wrong storage slot, a wrong optimization.
- The code passed review, because reviewers read the source, not the bytecode.
- An attacker who knows the bug finds the contracts that were built with that version, and uses the bug as if it were a normal code flaw.
Vulnerable and fixed code
# Vyper 0.2.15, 0.2.16 and 0.3.0
@external
@nonreentrant("lock")
def remove_liquidity(...):
... # the lock used a different storage slot per function,
# so cross-function reentrancy was possible// Pin a compiler version that has no known bug that affects your code.
pragma solidity 0.8.28; // exact version, not ^0.8.0
// Read the list of known bugs for that version:
// https://docs.soliditylang.org/en/latest/bugs.html
// For Vyper: https://github.com/vyperlang/vyper/security/advisoriesReview checklist
- Pin an exact compiler version. Do not use a floating pragma in deployed code.
- Read the known-bugs list of the compiler version before deployment.
- Do not use the newest compiler release for high-value code on its first weeks. Do not use very old versions either.
- Keep a list of your deployed contracts and their compiler versions, so you can check them when a new advisory comes out.
How to test for it
- Test the behaviour of the deployed bytecode, not only the source: run your reentrancy tests against the compiled artifact.
Real exploits (1)
-
Curve Finance (alETH, msETH, pETH, CRV/ETH pools; affecting JPEG'd, Alchemix, Metronome): Curve pools and partner protocols (JPEG'd, Alchemix, Metronome) lost about $69M because a 0-day bug in older Vyper versions broke the nonreentrant guard.
Root cause and fix
Vulnerable code:
Curve pools written in Vyper 0.2.15, 0.2.16 and 0.3.0 that hold native ETH (alETH/msETH/pETH/CRV-ETH) · add_liquidity / remove_liquidity (nonreentrant guard)Vyper versions 0.2.15, 0.2.16 and 0.3.0 had a compiler bug. It gave the nonreentrant lock different storage slots in different functions, such as add_liquidity and remove_liquidity, so the guard did not work. An attacker re-entered the pool between these functions while it was in an inconsistent state. This let them manipulate the LP token price and drain the pools that held native ETH.
What would have stopped it: Pin and upgrade to compiler versions that have been patched (0.3.1 or later), and watch compiler security advisories. Test the compiled bytecode, for example by checking that the lock slot is the same across all guarded functions, with reentrancy tests that run on the final build and not only on the source.