stale pointers are the new black

63
Stale pointers are the new black Vincenzo Iozzo, Giovanni Gola [email protected] [email protected]

Upload: vincenzo-iozzo

Post on 22-May-2015

350 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Stale pointers are the new black

Stale  pointers  are  the  new  black  

Vincenzo  Iozzo,  Giovanni  Gola  [email protected]  

[email protected]  

Page 2: Stale pointers are the new black

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  

Page 3: Stale pointers are the new black

Stale  pointers  are  the  new  black  

Page 4: Stale pointers are the new black

MoEvaEons  

Page 5: Stale pointers are the new black

Different  approaches  then..  

Page 6: Stale pointers are the new black

..  Or  StaEc  Analysis!  

• Dataflow  analysis  • Model  Checking  • Theorem  Proving  

Page 7: Stale pointers are the new black

Our  Idea  

Page 8: Stale pointers are the new black

How  it  works  

SSA  Form  Intraprocedural  pointer  analysis  

Interprocedural  pointer  analysis  

Bug  detecEon  

Page 9: Stale pointers are the new black

Single  StaEc  Assignment  Form  

Page 10: Stale pointers are the new black

Example  

mov  eax,  0x100  mov  ebx,  0x2  cmp  ecx,  edx  

Jle  1  

mov  ebx,  0x4   mov  ebx,  0x20  

mov  eax,  0x40  

Page 11: Stale pointers are the new black

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  

Page 12: Stale pointers are the new black

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_?  

Page 13: Stale pointers are the new black

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  

Page 14: Stale pointers are the new black

Intermediate  language  interlude  

Page 15: Stale pointers are the new black

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  

Page 16: Stale pointers are the new black

Example  

Page 17: Stale pointers are the new black

Translated  

Page 18: Stale pointers are the new black

Back  to  SSA    

Page 19: Stale pointers are the new black

Flavours  

• Non-­‐pruned  • Semi-­‐pruned  

• Pruned  

Page 20: Stale pointers are the new black

Non-­‐locals  

ldm  0x1000,  t0  add  t0,  t1,  t2  ldm  t2,  ,  t3  

ldm  t0,  t4  t0  is  a  non-­‐local    

Page 21: Stale pointers are the new black

Algorithm  

• Find  non-­‐locals  • Place  phi-­‐funcEons  • Recursively  rename  variables  

Page 22: Stale pointers are the new black

A  funcEon  

Page 23: Stale pointers are the new black

In  SSA  Form  

Page 24: Stale pointers are the new black

Detour..  Abstract  interpretaEon  

Page 25: Stale pointers are the new black

Abstract  InterpretaEon  

Page 26: Stale pointers are the new black

Abstract  InterpretaEon..  formally  

  Give  several  semanEcs  linked  by  relaEons  of       abstracEon    

Page 27: Stale pointers are the new black

MonoREIL  

Page 28: Stale pointers are the new black

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  

Page 29: Stale pointers are the new black

One  constraint!  

  The  lahce  has  to  saEsfy  the  ascending  chain       condiEon  

Page 30: Stale pointers are the new black

Now  the  analysis  itself  

Page 31: Stale pointers are the new black

Intraprocedural  Analysis  

•  Pointer  Analysis:  Efficiency  

•  Shape  Analysis:  Precision  

•  Alias  Set  Analysis:  Tradeoff  between  the  two  

Page 32: Stale pointers are the new black

Data  Structures  

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

•  Hash  consing:  30%  memory  saving  

Page 33: Stale pointers are the new black

Transfer  FuncEons  

Page 34: Stale pointers are the new black

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  

Page 35: Stale pointers are the new black

Data  Structures  Again  

Page 36: Stale pointers are the new black

Example  

Page 37: Stale pointers are the new black

Tracking  parameters  and  return  

•  IDA  effecEvely  tracks  parameters  

•  return  is  idenEfied  by  guessing  the  calling  convenEon  

Page 38: Stale pointers are the new black

Interprocedural  Analysis  

Page 39: Stale pointers are the new black

•  Flow-­‐insensiEve  

•  Context-­‐sensiEve  

•  Implemented  in  BinNavi:  

•  walks  on  the  PCG  

Algorithm  

Page 40: Stale pointers are the new black

Procedure  Call  Graph  

Page 41: Stale pointers are the new black

TransformaEons  

Page 42: Stale pointers are the new black

TransformaEons  

Page 43: Stale pointers are the new black

TransformaEons  

Page 44: Stale pointers are the new black

TransformaEons  

Page 45: Stale pointers are the new black

combine()  

Page 46: Stale pointers are the new black

Bug  DetecEon  

Page 47: Stale pointers are the new black

Callgraph  pruning  

Page 48: Stale pointers are the new black

Callgraph  pruning  

Page 49: Stale pointers are the new black

Callgraph  pruning  

Page 50: Stale pointers are the new black

Marking  destructor()  calls  

Page 51: Stale pointers are the new black

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  

Page 52: Stale pointers are the new black

Example  No  bugs  

Page 53: Stale pointers are the new black

Example  No  bugs  

Page 54: Stale pointers are the new black

Example  Use  amer  free  bug  

Page 55: Stale pointers are the new black

Example  Use  amer  free  bug  

Page 56: Stale pointers are the new black

Example  Use  amer  free  bug  (maybe)  

Page 57: Stale pointers are the new black

Example  Use  amer  free  bug  (maybe)  

Page 58: Stale pointers are the new black

Example  No  bugs  

Page 59: Stale pointers are the new black

Example  Use  amer  free  bug  

Page 60: Stale pointers are the new black

Example  Use  amer  free  bug  (maybe)  

Page 61: Stale pointers are the new black

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  

Page 62: Stale pointers are the new black

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  

Page 63: Stale pointers are the new black

That’s  all  folks