Security analysis of smart contracts in Datalog https://securify.ch - - PowerPoint PPT Presentation

security analysis of smart contracts in datalog
SMART_READER_LITE
LIVE PREVIEW

Security analysis of smart contracts in Datalog https://securify.ch - - PowerPoint PPT Presentation

Security analysis of smart contracts in Datalog https://securify.ch Dr. Petar Tsankov Senior researcher, SRI lab, ETH Zurich Co-founder and Chief Scientist, ChainSecurity Inter-disciplinary research at Next-generation blockchain security ETH


slide-1
SLIDE 1

Security analysis of smart contracts in Datalog

https://securify.ch

  • Dr. Petar Tsankov

Senior researcher, SRI lab, ETH Zurich Co-founder and Chief Scientist, ChainSecurity

slide-2
SLIDE 2

Inter-disciplinary research at ETH Zurich

Security and privacy Blockchain security Safety of AI

Next-generation blockchain security using automated reasoning

https://chainsecurity.com @chain_security

slide-3
SLIDE 3

Why do we need reliable smart contracts?

slide-4
SLIDE 4

Smart contract bugs bugs in the news

last month

2

slide-5
SLIDE 5

Vision to secure smart contracts

Writing secure contracts is hard Audits are manual and miss issues Most anomalies are invisible Pr Problem

Code audit Post-deployment Development

Ou Our solution Au Autom

  • mated

to tools Ma Machine-ch check cked au audit its Mo Monitori ring to tools

3

slide-6
SLIDE 6

SE SECURITY Y SC SCANNER

  • Discovers ge

gene neric ic vu vulnerabiliti ties

  • Supports Ethereum

and Hyperledger SY SYMBOLIC VERIFIER

  • Supports cu

custom pr prope pertie ies

  • Certifies

correctness AI AI-BA BASED TESTING

  • Generates high

high co coverage tests

  • Learns from data

(contracts and transactions)

Our core technology

4

slide-7
SLIDE 7

June 2016: The DAO hack

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 foo() { dao.withdraw(); } ... withdraw()

10 ether

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

0 ether calls the default "fallback” function

Later…

balance is zeroed after transfer

5

slide-9
SLIDE 9

5

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 foo() { 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

6

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?

7

Unsafe call instruction Safe call instruction

No, smart contracts are Turing-complete

slide-13
SLIDE 13

Insight

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

slide-14
SLIDE 14

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-15
SLIDE 15

www.securify.ch

Scalable and fully automated verifier for Ethereum smart contracts

slide-16
SLIDE 16

Impact

1K+ subscribers Used daily by security auditors (30K+ contracts scanned so far) Grants: Startup:

8

slide-17
SLIDE 17

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

Suitable for analysis Relevant semantic information Patterns written in a DSL

9

slide-18
SLIDE 18

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-19
SLIDE 19

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-20
SLIDE 20

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-21
SLIDE 21

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)

IR Datalog input Datalog fixpoint

11

slide-22
SLIDE 22

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-23
SLIDE 23

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-24
SLIDE 24

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-25
SLIDE 25

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-26
SLIDE 26

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-27
SLIDE 27

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-28
SLIDE 28

Evaluation

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

15

slide-29
SLIDE 29

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-30
SLIDE 30

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-31
SLIDE 31

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-32
SLIDE 32

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 Precise security patterns

Unsafe behaviors Safe behaviors Violation Warning Safe