Securify: Practical Security Analysis of Smart Contracts - - PowerPoint PPT Presentation

securify practical security analysis of smart contracts
SMART_READER_LITE
LIVE PREVIEW

Securify: Practical Security Analysis of Smart Contracts - - PowerPoint PPT Presentation

Securify: Practical Security Analysis of Smart Contracts https://securify.ch Dana Martin Petar Andrei Arthur Florian Drachsler- Vechev Tsankov Dan Gervais Bnzli Cohen Grant: Startup: What is a smart contract? mapping(address


slide-1
SLIDE 1

Securify: Practical Security Analysis of Smart Contracts

https://securify.ch

Andrei Dan Petar Tsankov Dana Drachsler- Cohen Arthur Gervais Florian Bünzli Martin Vechev Grant: Startup:

slide-2
SLIDE 2

2

What is a smart contract?

mapping(address => uint) balances; function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

  • Small programs that handle cryptocurrencies
  • Written in high-level languages (e.g., Solidity)
  • Usually no patching after release

What can go wrong when programs handle billions worth of USD?

Transfer funds to the caller

slide-3
SLIDE 3

Smart contract bugs bugs in the news

3

slide-4
SLIDE 4

Smart contract bugs bugs in the news

3

slide-5
SLIDE 5

Smart contract bugs bugs in the news

1 week ago

3

slide-6
SLIDE 6

June 2016: The DAO hack

slide-7
SLIDE 7

The DAO hack

DAO Contract

mapping(address => uint) balances; function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

User Contract

function moveBalance() { dao.withdraw(); } ... withdraw()

10 ether

function () payable { // log payment } withdraw()

0 ether

Later… 4

slide-8
SLIDE 8

The DAO hack

DAO Contract

mapping(address => uint) balances; function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

User Contract

function moveBalance() { dao.withdraw(); } ... withdraw()

10 ether

function () payable { // log payment } withdraw()

0 ether calls the default "fallback” function

Later…

balance is zeroed after transfer

4

slide-9
SLIDE 9

4

The DAO hack

DAO Contract

mapping(address => uint) balances; function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

User Contract

function moveBalance() { dao.withdraw(); } ...

...

calls withdraw() before balance is set to 0

function () payable { dao.withdraw(); } withdraw()

10 ether

withdraw()

10 ether

slide-10
SLIDE 10

Many critical vulnerabilities

Transaction reordering Reentrant method calls Unprivileged writes Unexpected ether flows Use of unsafe inputs

5

In 2017, more than

$300M

have been lost due to these issues

slide-11
SLIDE 11

Wanted: Automated security analysis

slide-12
SLIDE 12

The DAO hack

function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

Security property: No state changes after call instructions Unsafe calls Safe calls Can we automatically find all unsafe calls?

6

slide-13
SLIDE 13

The DAO hack

function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

Security property: No state changes after call instructions Unsafe calls Safe calls Can we automatically find all unsafe calls?

6

No, smart contracts are Turing-complete

slide-14
SLIDE 14

The DAO hack

function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

Security property: No state changes after call instructions Unsafe calls Safe calls Can we automatically find all unsafe calls? Existing solutions focus on bug finding and can miss issues

6

Unsafe call instruction Safe call instruction

No, smart contracts are Turing-complete

slide-15
SLIDE 15

Insight

When contracts satisfy/violate a security property, they often satisfy/violate a simpler property

slide-16
SLIDE 16

The DAO hack

function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

Verifies 91% of all calls Security property: No state changes after call instructions Unsafe calls Safe calls

A write always follows call.value() No writes may follow call.value()

7

Violation pattern Compliance pattern

slide-17
SLIDE 17

www.securify.ch

Scalable and fully automated verifier for Ethereum smart contracts

slide-18
SLIDE 18

Impact

1K+ subscribers Used daily by security auditors (29K+ contracts scanned so far) Grants: New startup:

8

slide-19
SLIDE 19

Securify: System overview

Intermediate representation

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) ⋮

Semantic representation

assign(1, a, 0x04) follow(2, 1) mayDepOn(b, a) load(2, b, a) follow(3,2) follow(5,3) ⋮

EVM bytecode

push 0x04 dataload push 0x08 jump jumpdest stop jumpdest ⋮

Security report

  • 2. infer facts
  • 1. decompile
  • 3. check patterns

9

slide-20
SLIDE 20

Step 1: Decompilation

Intermediate representation

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) ⋮

EVM bytecode

push 0x04 dataload push 0x08 jump jumpdest stop jumpdest ⋮

  • 1. decompile
  • Static single assignment form
  • Control-flow graph recovery

10

slide-21
SLIDE 21

Step 2: Inferring semantic facts

Intermediate representation

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) ⋮

Semantic representation

assign(1, a, 0x04) follow(2, 1) mayDepOn(b, a) load(2, b, a) follow(3,2) follow(5,3) ⋮

  • 2. infer facts
slide-22
SLIDE 22

Step 2: Inferring semantic facts

!"#$%&&%' (, * ← $%&&%'((, *) !"#$%&&%' (, * ← $%&&%' (, . , !"#$%&&%'(., *)

Scalable inference of semantic facts using Datalog solvers Datalog program

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) ⋮

IR

$%&&%'(2, 1) $%&&%'(3, 2) $%&&%'(5, 3) $%&&%'(6, 5) $%&&%'(4, 6)

Datalog input

!"#$%&&%'(2, 1) !"#$%&&%'(3, 1) !"#$%&&%'(4, 1) !"#$%&&%'(5, 1) !"#$%&&%'(6, 1)

Datalog fixpoint

11

slide-23
SLIDE 23

Step 2: Inferring semantic facts

!"#$%&&%' (, * ← $%&&%'((, *) !"#$%&&%' (, * ← $%&&%' (, . , !"#$%&&%'(., *) $%&&%'(2, 1) $%&&%'(3, 2) $%&&%'(5, 3) $%&&%'(6, 5) $%&&%'(4, 6)

Scalable inference of semantic facts using Datalog solvers

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) ⋮

!"#$%&&%'(2, 1) !"#$%&&%'(3, 1) !"#$%&&%'(4, 1) !"#$%&&%'(5, 1) !"#$%&&%'(6, 1)

Datalog program IR Datalog input Datalog fixpoint

Control-flow analysis 6"#$%&&%'(78, 79) Instruction at label 78 may follow that at label 79 6:;<$%&&%'(78, 79) Instruction at label 78 must follow that at label 79 Data-flow analysis 6"#=>?@A(B, C) The value of B may depend on tag C >D(B, C) The values of B and C are equal E><F#(B, C) For different values of C the value of B is different

Relevant semantic facts

11

For real-world contracts, Securify infers 1 - 10M such facts

slide-24
SLIDE 24

Step 3: Check patterns

Semantic representation

assign(1, a, 0x04) follow(2, 1) mayDepOn(b, a) load(2, b, a) follow(3,2) follow(5,3) ⋮

Security report

  • 3. check patterns
slide-25
SLIDE 25

Security patterns language

!" X, T &!'() X, Y | ,-).!/01(X, Y)

4 ∷= 718'9 :, ;, <, … , <

>?@@?A :, : | ,-)B?@@?A :, : ,C8'B?@@?A(:, :) ∃<. 4 ∃:. 4 ∃F. 4 ¬4 | 4 ∧ 4

A pat pattern is a logical formula over semantic predicates:

12 see paper for details

slide-26
SLIDE 26

13

Example: No writes after calls

!" ≡ ∀ %&'' (), _, _ . ¬∃ //0123 (4, _, _ . 5&671''18((4, ())

Compliance pattern

!" ≡ ∃ %&'' (), _, _ . ∃ //0123 (4, _, _ . 5;/071''18((4, ())

Violation pattern

function withdraw() { uint amount = balances[msg.sender]; msg.sender.call.value(amount)(); balances[msg.sender] = 0; }

! ≡ “No state changes after call instructions”

Security property: We can (manually) prove that: !" ⇒ ! and != ⇒ ¬!

slide-27
SLIDE 27

Security report

Unsafe calls Safe calls Violation pattern Compliance pattern All unsafe calls are reported as either vi violations or wa warnings

14

Violation Warning Safe

!" !# ¬! !

slide-28
SLIDE 28

Security report

Unsafe behaviors Safe behaviors Violation pattern Compliance pattern All unsafe behaviors are reported as either vi violations or wa warnings

14

Violation Warning Safe

!" !# ¬! !

Patterns for relevant security properties

slide-29
SLIDE 29

Evaluation

  • 1. Is Securify precise for relevant security properties?
  • 2. How does Securify compare to other contract checkers?

15

slide-30
SLIDE 30

How precise is Securify?

Dataset

  • First 100 real-world contracts uploaded to https://securify.ch

in 2018 Security properties

  • 9 critical vulnerabilities (reentrancy, …)

Experiment:

  • Measure % of violations, safe behaviors, and warnings
  • Manually classify warnings into true warnings and false

warnings

16

slide-31
SLIDE 31

How precise is Securify?

TT TR TA NW RW HE VA RT LQ False warnings True warnings Violations Security properties 20 40 80 60 100 % of all potential vulnerabilities

16

< 10% warnings for 6 out of 9 security properties

slide-32
SLIDE 32

How precise is Securify?

TT TR TA NW RW HE VA RT LQ False warnings True warnings Violations Security properties 20 40 80 60 100 % of all potential vulnerabilities

16

< 10% warnings for 6 out of 9 security properties

No warnings No warnings > 90% verified

slide-33
SLIDE 33

How does Securify compare to other checkers?

20 40 60 20 40 80 60

False warnings True warnings Violations Unreported vulnerabilities Oyente Mythril TOD Reentrancy Unhandled exception Unsafe transfer

17

slide-34
SLIDE 34

How does Securify compare to other checkers?

20 40 60 20 40 80 60

False warnings True warnings Violations Unreported vulnerabilities Oyente Mythril TOD Reentrancy Unhandled exception Unsafe transfer

17

> 50% false negatives Fewer false warnings

slide-35
SLIDE 35

Try online: https://securify.ch

TT TR TA NW RW HE VA RT LQ

1: a = 0x04 2: b = load(a) 3: abi_00(b) 4: stop abi_00(b) 5: c = 0x00 6: sstore(c,b) assign(1, a, 0x04) follow(2, 1) mayDepOn(b, a) load(2, b, a) follow(3,2) follow(5,3) push 0x04 dataload push 0x08 jump jumpdest stop jumpdest

Summary

High precision on real contracts Scalable automated analysis Domain-specific patterns

Unsafe behaviors Safe behaviors Violation Warning Safe