Stale pointers are the new black Vincenzo Iozzo, Giovanni - - PowerPoint PPT Presentation

stale pointers are the new black
SMART_READER_LITE
LIVE PREVIEW

Stale pointers are the new black Vincenzo Iozzo, Giovanni - - PowerPoint PPT Presentation

Stale pointers are the new black Vincenzo Iozzo, Giovanni Gola vincenzo.iozzo@zynamics.com giovanni.gola@mail.polimi.it Disclaimer In this talk you wont see all


slide-1
SLIDE 1

Stale ¡pointers ¡are ¡the ¡new ¡black ¡

Vincenzo ¡Iozzo, ¡Giovanni ¡Gola ¡

vincenzo.iozzo@zynamics.com ¡ giovanni.gola@mail.polimi.it ¡

slide-2
SLIDE 2

Disclaimer ¡

You ¡don’t ¡want ¡slides ¡like ¡ this, ¡do ¡you? ¡

In ¡this ¡talk ¡you ¡won’t ¡see ¡all ¡those ¡formulas, ¡formal ¡definiEon, ¡code ¡snippets ¡ and ¡bullets. ¡ ¡ From ¡past ¡experiences ¡the ¡speaker ¡learned ¡that ¡all ¡the ¡aforemenEoned ¡ elements ¡are ¡no ¡useful ¡in ¡making ¡people ¡understand ¡your ¡idea. ¡ You ¡instead ¡will ¡see ¡a ¡lot ¡of ¡funny ¡pictures ¡which ¡the ¡speaker ¡hopes ¡will ¡ convey ¡beHer ¡the ¡understanding ¡of ¡the ¡ideas ¡explained ¡in ¡the ¡talk ¡

slide-3
SLIDE 3

Stale ¡pointers ¡are ¡the ¡new ¡black ¡

slide-4
SLIDE 4

MoEvaEons ¡

slide-5
SLIDE 5

Different ¡approaches ¡then.. ¡

slide-6
SLIDE 6

.. ¡Or ¡StaEc ¡Analysis! ¡

  • Dataflow ¡analysis ¡
  • Model ¡Checking ¡
  • Theorem ¡Proving ¡
slide-7
SLIDE 7

Our ¡Idea ¡

slide-8
SLIDE 8

How ¡it ¡works ¡

SSA ¡Form ¡ Intraprocedural ¡ pointer ¡analysis ¡ Interprocedural ¡ pointer ¡analysis ¡ Bug ¡detecEon ¡

slide-9
SLIDE 9

Single ¡StaEc ¡Assignment ¡Form ¡

slide-10
SLIDE 10

Example ¡

mov ¡eax, ¡0x100 ¡ mov ¡ebx, ¡0x2 ¡ cmp ¡ecx, ¡edx ¡ Jle ¡1 ¡ mov ¡ebx, ¡0x4 ¡ mov ¡ebx, ¡0x20 ¡ mov ¡eax, ¡0x40 ¡

slide-11
SLIDE 11

Looks ¡beHer, ¡right? ¡

mov ¡eax_1, ¡0x100 ¡ mov ¡ebx_0, ¡0x2 ¡ cmp ¡ecx, ¡edx ¡ Jle ¡1 ¡ mov ¡ ¡ebx_1, ¡0x4 ¡ mov ¡ebx_2, ¡0x20 ¡ mov ¡eax_0, ¡0x40 ¡

slide-12
SLIDE 12

How ¡about ¡now? ¡

Mov ¡eax_1, ¡0x100 ¡ Mov ¡ebx_0, ¡0x2 ¡ cmp ¡ecx, ¡edx ¡ Jle ¡1 ¡ mov ¡ebx_1, ¡0x4 ¡ mov ¡ebx_2, ¡0x20 ¡ mov ¡eax_0, ¡0x40 ¡ mov ¡ ¡ecx, ¡ebx_? ¡

slide-13
SLIDE 13

Tah-­‑dah! ¡

mov ¡eax_1, ¡0x100 ¡ mov ¡ebx_0, ¡0x2 ¡ cmp ¡ecx, ¡edx ¡ Jle ¡1 ¡ mov ¡ ¡ebx_1, ¡0x4 ¡ mov ¡ebx_2, ¡0x20 ¡ mov ¡eax_0, ¡0x40 ¡ mov ¡0x4, ¡ ebx_return_from_phi ¡

slide-14
SLIDE 14

Intermediate ¡language ¡interlude ¡

slide-15
SLIDE 15

A ¡small ¡introducEon ¡to ¡the ¡REIL ¡meta ¡language ¡

  • ¡small ¡RISC ¡instrucEon ¡set ¡(17 ¡instrucEons) ¡ ¡
  • ¡ArithmeEc ¡instrucEons ¡(ADD, ¡SUB, ¡MUL, ¡DIV, ¡MOD, ¡BSH) ¡
  • ¡Bitwise ¡instrucEons ¡(AND, ¡OR, ¡XOR) ¡
  • ¡Logical ¡instrucEons ¡(BISZ, ¡JCC) ¡
  • ¡Data ¡transfer ¡instrucEons ¡(LDM, ¡STM, ¡STR) ¡
  • ¡Other ¡instrucEons ¡(NOP, ¡UNDEF, ¡UNKN) ¡
  • ¡register ¡machine ¡ ¡
  • ¡unlimited ¡number ¡of ¡temp ¡registers ¡
  • ¡side ¡effect ¡free ¡
  • ¡no ¡excepEons, ¡floaEng ¡point, ¡64Bit, ¡.. ¡

Enter ¡REIL ¡

slide-16
SLIDE 16

Example ¡

slide-17
SLIDE 17

Translated ¡

slide-18
SLIDE 18

Back ¡to ¡SSA ¡ ¡

slide-19
SLIDE 19

Flavours ¡

  • Non-­‑pruned ¡
  • Semi-­‑pruned ¡
  • Pruned ¡
slide-20
SLIDE 20

Non-­‑locals ¡

ldm ¡0x1000, ¡t0 ¡ add ¡t0, ¡t1, ¡t2 ¡ ldm ¡t2, ¡, ¡t3 ¡ ldm ¡t0, ¡t4 ¡ t0 ¡is ¡a ¡ non-­‑ local ¡ ¡

slide-21
SLIDE 21

Algorithm ¡

  • Find ¡non-­‑locals ¡
  • Place ¡phi-­‑funcEons ¡
  • Recursively ¡rename ¡variables ¡
slide-22
SLIDE 22

A ¡funcEon ¡

slide-23
SLIDE 23

In ¡SSA ¡Form ¡

slide-24
SLIDE 24

Detour.. ¡Abstract ¡interpretaEon ¡

slide-25
SLIDE 25

Abstract ¡InterpretaEon ¡

slide-26
SLIDE 26

Abstract ¡InterpretaEon.. ¡formally ¡

¡Give ¡several ¡semanEcs ¡linked ¡by ¡relaEons ¡of ¡ ¡ ¡abstracEon ¡ ¡

slide-27
SLIDE 27

MonoREIL ¡

slide-28
SLIDE 28

So ¡what ¡you ¡need? ¡

  • The ¡control ¡flow ¡graph ¡of ¡a ¡funcEon ¡
  • A ¡way ¡to ¡walk ¡the ¡CFG ¡
  • The ¡lahce ¡ ¡

– Its ¡elements ¡ – A ¡way ¡to ¡combine ¡lahce ¡elements ¡

  • An ¡iniEal ¡state ¡
  • REIL ¡instrucEons ¡effects ¡on ¡the ¡lahce ¡
slide-29
SLIDE 29

One ¡constraint! ¡

¡The ¡lahce ¡has ¡to ¡saEsfy ¡the ¡ascending ¡chain ¡ ¡ ¡condiEon ¡

slide-30
SLIDE 30

Now ¡the ¡analysis ¡itself ¡

slide-31
SLIDE 31

Intraprocedural ¡Analysis ¡

  • Pointer ¡Analysis: ¡Efficiency ¡
  • Shape ¡Analysis: ¡Precision ¡
  • Alias ¡Set ¡Analysis: ¡Tradeoff ¡between ¡the ¡

two ¡

slide-32
SLIDE 32

Data ¡Structures ¡

  • push() ¡and ¡pop() ¡on ¡linked ¡lists: ¡30% ¡

faster ¡

  • Hash ¡consing: ¡30% ¡memory ¡saving ¡
slide-33
SLIDE 33

Transfer ¡FuncEons ¡

slide-34
SLIDE 34

combine() ¡

  • Filter ¡out ¡non-­‑live ¡variables ¡from ¡each ¡alias ¡

list: ¡

  • live-­‑out(inst) ¡⊆ ¡vars(dom(inst)) ¡
  • Alias ¡list ¡∩ ¡vars(sdom(Φ)): ¡
  • pop() ¡from ¡the ¡list ¡unEl ¡ ¡

top(alias ¡list) ¡∈ ¡vars(sdom(Φ)) ¡

  • Add ¡aliases ¡defined ¡by ¡Φ ¡funcEons ¡
  • Unite ¡the ¡sets ¡of ¡lists ¡
slide-35
SLIDE 35

Data ¡Structures ¡Again ¡

slide-36
SLIDE 36

Example ¡

slide-37
SLIDE 37

Tracking ¡parameters ¡and ¡return ¡

  • IDA ¡effecEvely ¡tracks ¡parameters ¡
  • return ¡is ¡idenEfied ¡by ¡guessing ¡the ¡

calling ¡convenEon ¡

slide-38
SLIDE 38

Interprocedural ¡Analysis ¡

slide-39
SLIDE 39
  • Flow-­‑insensiEve ¡
  • Context-­‑sensiEve ¡
  • Implemented ¡in ¡BinNavi: ¡
  • walks ¡on ¡the ¡PCG ¡

Algorithm ¡

slide-40
SLIDE 40

Procedure ¡Call ¡Graph ¡

slide-41
SLIDE 41

TransformaEons ¡

slide-42
SLIDE 42

TransformaEons ¡

slide-43
SLIDE 43

TransformaEons ¡

slide-44
SLIDE 44

TransformaEons ¡

slide-45
SLIDE 45

combine() ¡

slide-46
SLIDE 46

Bug ¡DetecEon ¡

slide-47
SLIDE 47

Callgraph ¡pruning ¡

slide-48
SLIDE 48

Callgraph ¡pruning ¡

slide-49
SLIDE 49

Callgraph ¡pruning ¡

slide-50
SLIDE 50

Marking ¡destructor() ¡calls ¡

slide-51
SLIDE 51

Algorithm ¡

  • ¡v ¡is ¡a ¡tracked ¡alias ¡
  • ¡X ¡is ¡a ¡basic ¡block ¡of ¡F ¡that ¡calls ¡the ¡destructor ¡
  • ¡B ¡is ¡a ¡basic ¡block ¡of ¡F ¡that ¡accesses ¡v ¡or ¡calls ¡a ¡funcEon ¡

that ¡accesses ¡v ¡

  • ¡Verify ¡the ¡following: ¡

 if ¡B ¡∈ ¡dom(X) ¡⇒ ¡v ¡is ¡a ¡stale ¡pointer ¡  if ¡B ¡!∈ ¡dom(X) ¡∧ ¡B ¡∈ ¡succ(X) ¡⇒ ¡v ¡may ¡be ¡a ¡stale ¡pointer ¡  if ¡X ¡!∈ ¡dom(B) ¡∧ ¡X ¡∈ ¡succ(B) ¡⇒ ¡v ¡may ¡cause ¡memory ¡leak ¡

  • if ¡X ¡!∈ ¡dom(B) ¡∧ ¡X ¡!∈ ¡succ(B) ¡⇒ ¡v ¡causes ¡memory ¡leak ¡
  • ¡Iterate ¡subsEtuEng: ¡
  • F ¡with ¡each ¡of ¡its ¡callers ¡
  • X ¡with ¡a ¡basic ¡block ¡that ¡calls ¡F ¡
slide-52
SLIDE 52

Example ¡

No ¡bugs ¡

slide-53
SLIDE 53

Example ¡

No ¡bugs ¡

slide-54
SLIDE 54

Example ¡

Use ¡amer ¡free ¡bug ¡

slide-55
SLIDE 55

Example ¡

Use ¡amer ¡free ¡bug ¡

slide-56
SLIDE 56

Example ¡

Use ¡amer ¡free ¡bug ¡(maybe) ¡

slide-57
SLIDE 57

Example ¡

Use ¡amer ¡free ¡bug ¡(maybe) ¡

slide-58
SLIDE 58

Example ¡

No ¡bugs ¡

slide-59
SLIDE 59

Example ¡

Use ¡amer ¡free ¡bug ¡

slide-60
SLIDE 60

Example ¡

Use ¡amer ¡free ¡bug ¡(maybe) ¡

slide-61
SLIDE 61

What’s ¡the ¡catch ¡

  • We ¡cannot ¡handle ¡all ¡data ¡structures ¡
  • We ¡cannot ¡handle ¡funcEon ¡pointers ¡
  • We ¡have ¡false ¡posiEves ¡
  • We ¡have ¡false ¡negaEves ¡
  • Some ¡“smart ¡pointers”-­‑like ¡interfaces ¡might ¡

not ¡be ¡covered ¡

  • The ¡best ¡use ¡is ¡for ¡C++ ¡life-­‑span ¡issues ¡
slide-62
SLIDE 62

Future ¡

  • Increase ¡the ¡number ¡of ¡covered ¡data ¡structure ¡
  • Use ¡a ¡solver ¡to ¡reduce ¡false ¡posiEves ¡
  • Import ¡dynamic ¡analysis ¡data ¡to ¡miEgate ¡the ¡

funcEon ¡pointers ¡problem ¡

slide-63
SLIDE 63

That’s ¡all ¡folks ¡