Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1...

56
CodeAlchemist: Semantics-Aware Code Generation to Find Vulnerabilities in JavaScript Engines HyungSeok Han, DongHyeon Oh, Sang Kil Cha KAIST

Transcript of Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1...

Page 1: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

CodeAlchemist:Semantics-Aware Code Generation to

Find Vulnerabilities in JavaScript EnginesHyungSeok Han, DongHyeon Oh, Sang Kil Cha

KAIST

Page 2: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

2

https://daramg.gift

Page 3: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

3

https://daramg.gift

# id

uid=0(root) gid=0(root) groups=0(root)

# _

Page 4: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

4

https://daramg.gift

# id

uid=0(root) gid=0(root) groups=0(root)

# _

$200,000

JS bugs are critical!

Page 5: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

How to Find JS Bugs?

1. Analyzing JS Engine Code

2. Fuzzing

5

Page 6: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

How to Find JS Bugs?

1. Analyzing JS Engine Code

2. Fuzzing

6

Page 7: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Structure of JS Engine

7

Parser Executor

JS engine

AST

Syntax Error Semantic Error

Result

Page 8: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Previous Work: Grammar-based Fuzzer

8

Parser Executor

JS engine

AST

Syntax Error Semantic Error

Result

Semantics-unaware

Page 9: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness

9

// ReferenceErrorvar arr = new Array (n);

// TypeErrorvar num = 10; num ();

ExistingJS Fuzzer

S-> aS|bXX-> aX|bYY-> aY|c

Page 10: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Previous Work: Grammar-based Fuzzer

• jsfunfuzz− A state-of-the-art generation-based fuzzer developed by Mozilla− Found 2,800 bugs since 2006

• LangFuzz− A state-of-the-art mutation-based fuzzer appeared at USENIX’12− A parent of IFuzzer and TreeFuzz− Found 2,300 bugs since 2011

10

Page 11: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of jsfunfuzz

100,000 JS code snippets

11

jsfunfuzz

JS code

try {var n = 42, buf = [x, 2, 3];…

} catch(e) {}

Suppress semantic errors

Page 12: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of jsfunfuzz

100,000 JS code snippets

12

jsfunfuzz

JS code

// try {var n = 42, buf = [x, 2, 3];…

// } catch(e) {}

Suppress semantic errors

Page 13: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of jsfunfuzz

13

Page 14: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Previous Work: Grammar-based Fuzzer

• jsfunfuzz− A state-of-the-art generation-based fuzzer developed by Mozilla− Found 2,800 bugs since 2006

• LangFuzz− A state-of-the-art mutation-based fuzzer appeared in USENIX’12− A parent of IFuzzer and TreeFuzz− Found 2,300 bugs since 2011

14

Page 15: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of LangFuzz

var arr = new Array (0x100);for(let i = 0; i < 0x100; i++){// i is only available herearr[i] = i;

}

15

Page 16: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of LangFuzz

var x = new String (y);for(let i = 0; i < 0x100; i++){// i is only available herearr[i] = i;

}

16

Page 17: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Unawareness of LangFuzz

var arr = new String (i);for(let i = 0; i < 0x100; i++){// i is only available herearr[i] = i;

}

17

ReferenceError: i is not defined

Page 18: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Our Goal: Be Semantics-Aware

18

Parser Executor

JS engine

AST

Syntax Error Semantic Error

Result

Page 19: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Intuition: Assemble Code Bricks by Assembly Constraints

19

Code bricksw/ constraints

Semantically valid code

Page 20: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

CodeAlchemist: Semantics-Aware Code Generation for Fuzzing

AnalyzerParser FuzzerCode Brick Pool

ASTs,Code Bricks

20

Assembly constraint analysis

Semantics-aware assembly

Page 21: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

How to Analyze Assembly Constraints?

AnalyzerParser FuzzerCode Brick Pool

ASTs,Code Bricks

21

Assembly constraint analysis

Semantics-aware assembly

Page 22: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint

22

Code brickAssemblyConstraint

Required vars & types

Available vars & types

Pre-cond

Post-cond

Page 23: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint

23

Available vars & types

Required vars & types

AssemblyConstraint

var n = 42, buf = [1, 2, 3];

Pre-cond

Post-cond

Page 24: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint

24

Available vars & types

empty

AssemblyConstraint

var n = 42, buf = [1, 2, 3];

Pre-cond

Post-cond

Page 25: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint

25

n: number, buf: Array

empty

AssemblyConstraint

var n = 42, buf = [1, 2, 3];

Pre-cond

Post-cond

Page 26: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint Analysis

26

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

var n = 42, buf = [1, 2, 3]; n: numberbuf: Arrayempty

?

?

?

?

Page 27: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Data-flow Analysis

27

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

?

?

?

?

Page 28: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Data-flow Analysis

28

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

n: ?

?

?

?

Page 29: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Data-flow Analysis

29

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

n: ?

?

n: ?arr: ?

?

Page 30: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Data-flow Analysis

30

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

n: ?

n: ?arr: ?

n: ?arr: ?

n: ?arr: ?

Page 31: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Dynamic Type Analysis

31

var arr = new Array (n);n: ? n: ?arr: ?

Page 32: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Dynamic Type Analysis

32

getType ({n:n});var arr = new Array (n);getType ({n:n, arr:arr});

n: ? n: ?arr: ?

Page 33: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Dynamic Type Analysis

33

n: number n: numberarr: Array

getType ({n:n});var arr = new Array (n);getType ({n:n, arr:arr});

Page 34: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Dynamic Type Analysis

34

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

n: ?arr: ?

n: ?arr: ?

n: number n: numberarr: Array

Page 35: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint Analysis

35

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;

n: numberbuf: Arrayempty

n: numberarr: Array

n: numberarr: Array

n: number n: numberarr: Array

Page 36: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

How to Assemble Code Bricks?

AnalyzerParser FuzzerCode Brick Pool

ASTs,Code Bricks

36

Assembly constraint analysis

Semantics-aware assembly

Page 37: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Code Bricks with Teeth & Holes

37

n: numberbuf: Arrayempty

n: numberarr: Array

n: numberarr: Array

n: number n: numberarr: Arrayvar arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;n: int arr: Array

n: int arr: Array

n: int

n: int arr: Array

n: int buf: Array

var n = 42, buf = [1, 2, 3];

Page 38: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Aware Assembly

38

var arr = new Array (n);

for (let i = 0; i < n; i++)arr[i] = i;n: int arr: Array

n: int arr: Array

n: int

n: int arr: Array

n: int buf: Array

var n = 42, buf = [1, 2, 3];

Page 39: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

var n = 42, buf = [1, 2, 3];var arr = new Array (n);

n: int arr: Array buf: Array

Semantics-Aware Assembly

39

n: int buf: Array

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);n: int

n: int arr: Array

Page 40: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Aware Assembly

40

for (let i = 0; i < n; i++)arr[i] = i;

n: int arr: Array

arr: Arrayn: int

var n = 42, buf = [1, 2, 3];var arr = new Array (n);

n: int arr: Array buf: Array

n: int arr: Array buf: Array

var n = 42, buf = [1, 2, 3];var arr = new Array (n);for (let i = 0; i < n; i++)arr[i] = i;

Page 41: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Evaluation

AnalyzerParser FuzzerCode Brick Pool

ASTs,Code Bricks

41

How valid?

How effective?

Page 42: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Experiment Setup

• Collect about 63,000 JS code snippets− Regression tests in four major JS engines− Test code snippets in Test262− PoC exploits for previous security bugs

• The latest JS engines as of July 10th, 2018− ChakraCore 1.10.1− V8 6.7.288.46− JavaScriptCore 2.20.3− SpiderMonkey 61.0.1

42

Page 43: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Validity of Generated JS

43

Page 44: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

vs. State-of-the-Arts

• jsfunfuzz: A state-of-the-art JS fuzzer developed by Mozilla• IFuzzer: A variant of LangFuzz, ESORICS’16• Running time: 24 hours x 4 engines = 96 hours

44

Page 45: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Real-World Bug Finding

• Ran a week for the latest JS engines− JavaScriptCore: 2.20.3, 2.21.4 (beta)− V8: 6.7.288.46− SpiderMonkey: 61.0.1− ChakraCore: 1.10.0, 1.10.1

• Found 19 unique bugs− 11 exploitable bugs− 3 CVEs for us

45

Page 46: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

46

$5.424 / hours x 24 hours x 7 days x 6 engines

= $5,500

$200,000 / exploitable bugs x 11 bugs

= $2,200,000

Amazon EC2 CodeAlchemist

Page 47: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Future Research

• Seed selection

• Simple random code brick selection

• Supporting other language interpreters or compilers

47

Page 48: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Open Science

https://github.com/SoftSec-KAIST/CodeAlchemist

48

Release in March

Page 49: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

More in the Paper

• Fragmentization

• Code brick generation

• Parameters of CodeAlchemist

• Other evaluation

49

Page 50: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Question?

50

Page 51: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Assembly Constraint

51

Available vars & types

Required vars & types

AssemblyConstraint

var n = 42, buf = [1, 2, 3];

Page 52: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

• Dynamic nature of JS

• Complex and large top-level statement

• Abstract assembly constraint

Why not 100% Success?

52

if (n < 42) y = 10;else y = [];n: number

n: numbery: number

n: numbery: Array

Page 53: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

JS Type CodeAlchemist handles

• Primitive types− Undefined, Null, String, Boolean, Symbol, Number, Object

• Built-in types− Array, ArrayBuffer, Function, …− Depend on JS engine

53

Page 54: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

vs. State-of-the-Arts (in Previous Ver.)

• Ran 24 hours for ChakraCore 1.7.6 (Jan. 9th, 2018)• jsfunfuzz: the latest version before Jan. 9th, 2018• Seeds: JS snippets before Jan. 9th, 2018

54

Page 55: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

Semantics-Aware Assembly

55

n: int buf: Array

var n = 42, buf = [1, 2, 3];

var arr = new Array (x);x: int

x: int arr: Array

Page 56: Semantics-Aware Code Generation to Find Vulnerabilities in ... · −ChakraCore: 1.10.0, 1.10.1 •Found 19 unique bugs −11 exploitable bugs −3 CVEs for us 45. 46 $5.424 / hours

var n = 42, buf = [1, 2, 3];var arr = new Array (n);

n: int arr: Array buf: Array

Semantics-Aware Assembly

56

n: int buf: Array

var n = 42, buf = [1, 2, 3];

var arr = new Array (n);n: int

n: int arr: Array