Level 3 · How it worksThe village#229

Smart contract risks: the rule does exactly what it says, including the mistake

A smart contract can lose your money without anyone stealing it: through a bug, through someone who holds an admin key, through a bad price feed, a fake website or a permission you signed. An audit lowers the risk but never removes it. Before you deposit, check who can change the contract and what happens if it breaks.

A rule in the village notebook runs by itself. That is the whole point: nobody can stop it, nobody can bend it, and it gives the same answer in every house.

It is also the whole risk. If the rule says the wrong thing, it does the wrong thing, perfectly, in every house at once. There is no clerk to ring and no manager to complain to.

Many losses in smart contracts need no genius at all. A rule with a hole in it is enough, or a rule someone else can rewrite after you've paid in.

What it is

Smart contract risk is the chance that money you put into a contract is lost, frozen or taken because of how the contract was written, who controls it, or what it relies on.

The Solidity documentation, the manual for Ethereum's main contract language, puts the problem in one sentence: "While it is usually quite easy to build software that works as expected, it is much harder to check that nobody can use it in a way that was not anticipated" (Solidity docs). It adds that every contract runs "in public", so anyone looking for the hole can read the rule first.

The five doors

Most of these have their own article. In one line each:

  • Bugs. The code does what it says, not what was meant. The DAO lost about a third of its ether this way in 2016.
  • Admin and upgrade keys. Someone can change the rule after you deposit. ethereum.org: "An entity with access to upgrade functions can change the entire contract" (ethereum.org).
  • Approvals. A permission you signed lets a contract move your tokens later, even years later.
  • Oracles. The contract trusts a price someone else writes. Push the price, and the rule follows.
  • Front-ends. The contract is fine, but the website that prepares your transaction is not.

How it works: the Parity freeze, November 2017

The clearest case of "nobody stole it, and it's gone anyway" comes from Parity, a company that made Ethereum software. Its own post-mortem gives the facts (Parity).

  1. The design. Parity's multi-signature wallets were small contracts that all relied on one shared "library" contract for their logic. Hundreds of wallets, one rulebook.
  2. 20 July 2017. After an attack on the wallets on 19 July, Parity deployed a new library contract. It kept a self-destruct function, and it "required initialization": someone had to claim it as owner.
  3. August 2017. A contributor suggested initialising the contract on deployment. Parity made the change in its code, but it had not been deployed.
  4. 6 November 2017. A user found the library still had no owner, made themselves owner and called self-destruct. The rulebook was deleted.
  5. The result. 587 wallets could no longer move their funds. Frozen: 513,774.16 ETH, plus tokens.

The sum: 513,774.16 divided by 587 is about 875 ETH per wallet on average. Every wallet still exists. Every balance is still in the notebook. The rule that let anyone spend it is gone.

Parity's first lesson in the post-mortem: remove kill functions you don't need. Without one, even someone who took ownership "would not have been able to do anything".

Why it matters

  • No helpdesk. A frozen bank account has a court and a regulator behind it. A frozen contract has whatever its code allows, and sometimes that is nothing.
  • Upgrades cut both ways. A contract that can be fixed can also be changed against you. ethereum.org notes: "Without timelocks, users need to trust developers not to implement arbitrary changes".
  • Who pays. The depositors, almost always. The code has no budget for refunds.
  • Who gains. Whoever finds the hole first, and the auditors, who get paid either way.

Where people get it wrong

  • "It's audited, so it's safe." An audit is a check by people at one point in time on one version of the code. The Solidity docs say their own list of pitfalls "can, of course, never be complete".
  • "Nobody can change a smart contract." Many can be upgraded. Find out who holds that key.
  • "It's been running for years." So had the Parity wallets' design. Age shows nobody found the hole yet, not that there isn't one.
  • "If it goes wrong, the network will fix it." Ethereum rewrote one page once, for The DAO in 2016, after a month of arguing. Don't build a plan on it happening for you.

A five-minute check before you deposit

Before you put money into any contract, look for these in the project's documentation and on a block explorer:

  1. Verified code. Is the contract's source code published and verified on a block explorer?
  2. Audits. Who audited it, when, and which version? Were the findings fixed?
  3. Admin keys. Can it be upgraded or paused? By one person, a multisig, or a vote? Is there a timelock that gives you time to leave?
  4. Price feeds. Where do its prices come from?
  5. Your exposure. Only deposit what you could lose completely, and revoke approvals you no longer need.

If the documentation can't answer the third question, that is an answer. Somebody may hold a pen that can rewrite your rule, and the page doesn't say who.

Frequently asked questions

What are the main risks of smart contracts?

The main risks are bugs in the code, admin or upgrade keys that let someone change the contract, token approvals that let contracts move your tokens later, faulty or manipulated price oracles, and hijacked websites. Each can cost money without any hack of the blockchain itself.

Does an audit make a smart contract safe?

No. An audit lowers the risk by having specialists check one version of the code at one point in time. It can miss problems, and it does not cover later upgrades or the website you use.

What happened in the Parity wallet freeze?

On 6 November 2017, a user took ownership of the shared library contract behind Parity's multi-signature wallets and self-destructed it. According to Parity's post-mortem, 587 wallets holding 513,774.16 ETH plus tokens could no longer move their funds.

What is an upgradeable smart contract?

An upgradeable contract can have its logic replaced after deployment, usually through a proxy controlled by an admin key, a multisig or a vote. It allows bugs to be fixed, but whoever controls the upgrade can also change the rules for everyone who deposited.

How can I check a smart contract before using it?

Check that its source code is verified on a block explorer, read who audited it and when, and find out who can upgrade or pause it and whether there is a timelock. Only deposit what you could afford to lose completely.

Sources

  1. Parity Technologies, A Postmortem on the Parity Multi-Sig Library Self-Destruct (15 November 2017)
  2. Solidity documentation, Security considerations
  3. ethereum.org, Upgrading smart contracts
  4. ethereum.org, The history of Ethereum

Read next.