Experiments · Nobody Owns Money · Chapter 23

The cash machine that forgets to debit

The reentrancy attack behind The DAO hack

Theme
Try itThe cash machine that forgets to debit
Two lines of code in two orders. Send the money first and record it after, and the recipient has time to call withdraw again, and again. Record first, and the second call finds an empty account.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

The DAO was a fund kept by a contract, and a member leaving it could withdraw their share. The withdrawal did two things in this order. It sent the money, and then it wrote down that the money had been sent. The attacker’s own contract, on receiving the payment, called withdraw again before the record was made, and the fund paid again. On 17 June 2016 about 3.6 million ether, roughly a third of the fund, left this way.

The experiment shows those two lines in both orders. The numbers are a toy, a fund of 100 ETH with the attacker’s share at 10. Press “Step” to go one event at a time or “To the end” to run it out, and watch the call stack grow on the side. Each call checks that the share is above zero, the fund sends 10 ETH, and the attacker’s contract calls back in before the share is set to zero.

Send first and record later, and the same share of 10 is paid out again and again until the fund has too little left for one more. Switch to “Record first, then send”, and the second call finds a zero share. The attacker gets his 10 ETH and nothing more. The whole fix is two lines swapped.

The contract did exactly what its code said, which is why the chapter is called “A Robbery by the Rules”. The widget’s own comparison is a cash machine that hands out notes and forgets to debit the account.

From the book

Nobody Owns Money · Chapter 23

A Robbery by the Rules

Next to this experiment in the chapter
Soon an open letter appeared online from someone who called himself the attacker. Its authenticity was never confirmed, but everyone remembered the argument. The author wrote that he had studied the code carefully, had used an opportunity the code provided, and considered the ether he got a legitimate reward, and that his lawyers had confirmed he had broken nothing. If code is law, then he had not broken the law. Any attempt to take the ether back he called theft.

The first chapters are free →