crcount: pointer invalidation with reference counting to

39
CRCount: Pointer Invalidation with Reference Counting to Mitigate Use-after-free in Legacy C/C++ Seoul National University Jangseop Shin, Donghyun Kwon, Jiwon Seo, Yunheung Paek Soongsil University Yeongpil Cho

Upload: others

Post on 30-Apr-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CRCount: Pointer Invalidation with Reference Counting to

CRCount: Pointer Invalidation with Reference Counting to Mitigate Use-after-free in Legacy C/C++

Seoul National UniversityJangseop Shin, Donghyun Kwon, Jiwon Seo, Yunheung Paek

Soongsil UniversityYeongpil Cho

Page 2: CRCount: Pointer Invalidation with Reference Counting to

Use-After-Free (UAF)

nextdata

nextdata

0x7fffc480

(a)

objA

objBnextdata

ptrB

(b)

objB

ptrX

nextdata

ptrB

(c)

objB

objX

ptrB

ptrX->b = 0x7fffc480…

ptrB->next->next??

ptrA

free(ptrA)

Page 3: CRCount: Pointer Invalidation with Reference Counting to

UAF Defenses – Access Validation

• Check every memory access

ptrBLock addr keyPointer metadata

ptrB_copyLock addr key

INVALID

free(ptrB)

CETS: Compiler-Enforced Temporal Safety for C, ACM Sigplan Notices, 2010

lock

objB

key

Page 4: CRCount: Pointer Invalidation with Reference Counting to

UAF Defenses – Pointer invalidation

• Invalidate pointers on free()• Track only when a pointer is stored, not on every

memory access

1

Dangsan: Scalable useafter-free detection, EuroSys 2017

Locations of pointers to objA

&1&2

2

SPEC2006Runtime + 44%

Memory + 126%

Too high for runtime protection

INVALIDATED

INVALIDATED

Page 5: CRCount: Pointer Invalidation with Reference Counting to

Revisit reference counting

• Goal: free memory only when all the dangling pointers are gone

nextdata

ptrA objA

nextdata

ptrB objB

RCobjA = 2

Page 6: CRCount: Pointer Invalidation with Reference Counting to

Revisit reference counting

• Goal: free memory only when all the dangling pointers are gone

ptrA

nextdata

ptrB objB

RCobjA = 2

Page 7: CRCount: Pointer Invalidation with Reference Counting to

Revisit reference counting

• Goal: free memory only when all the dangling pointers are gone

ptrA

nextdata

ptrB objB

RCobjA = 1

dec_rc_store

Page 8: CRCount: Pointer Invalidation with Reference Counting to

Revisit reference counting

• Goal: free memory only when all the dangling pointers are gone

ptrA

ptrB objB

RCobjA = 0

0dec_rc_free

Page 9: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

ptrA

ptrB objB

RCobjA = 3

nextdatachunk

nextdata

objA

next/dataobjCptrC

Page 10: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

ptrA

ptrB objB

RCobjA = 2

chunk

nextdata

objA

next/dataobjCptrC

dec_rc_store

Page 11: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

ptrA

ptrB objB

RCobjA = 2

chunk

nextdata

objA

next/dataobjCptrC dec_rc_free

Page 12: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

ptrA

ptrB objB

RCobjA = 2

chunk

nextdata

objA

1objCptrC

Page 13: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

• How to know where the pointers are stored

• How to find right instrumentation points• Instrumenting only store instructions that have to do

with the pointers

Page 14: CRCount: Pointer Invalidation with Reference Counting to

Overview of our approach

Data Structures Runtime Library

Compiler Plugin

Page 15: CRCount: Pointer Invalidation with Reference Counting to

Data Structures

Pointer bitmap(shadow memory)

Pointer bitmap

“1” indicates heap pointer is currently

stored

Ptr-to-md map

MetAllocObject

metadata

MetAlloc≈ 2-level page table

Metalloc: Efficient and comprehensive metadata management for software securityHardening, European Workshop on System Security, 2016

ptr

Object metadata

Page 16: CRCount: Pointer Invalidation with Reference Counting to

Runtime Library

• crc_alloc, crc_free è function hooks• others è Instrumented by the compiler

Page 17: CRCount: Pointer Invalidation with Reference Counting to

crc_alloc

Object allocated

(1) Allocate object metadata

(2) register ptr-to-md map

nextdata

objA

0&objA

Ptr-to-md map

Page 18: CRCount: Pointer Invalidation with Reference Counting to

crc_store

Ptr-to-md map

(1) Check if heap ptr was stored therenext

data

ptrA

objA

some_obj

(3) Decrease RCsom_obj (free some_obj if RC = 0)

1 è 0&some_objPointer bitmap

(2) If yes, find md for some_obj

Page 19: CRCount: Pointer Invalidation with Reference Counting to

crc_store

(3) Increase RCobjA

(2) Find md for objAnext

data

objA

0 è 1&objA

(1) Set ptr bitmapPointer bitmap

Ptr-to-md map

ptrA

Page 20: CRCount: Pointer Invalidation with Reference Counting to

crc_free

(3) Decrease corresponding RC(free objA)

(2) Find md for those ptrs

next

data

objA

1 è 0&objA

(1) Find ptrs in objB

Pointer bitmap

Ptr-to-md map

next è 0

data

objB

ptrA

(4) Nullify the ptrs(5) Free objB only if RC == 0

Page 21: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

• How to know where the pointers are stored

• How to find right instrumentation points• Instrumenting only store instructions that have to do

with the pointers

Page 22: CRCount: Pointer Invalidation with Reference Counting to

Compiler plugin

• Selectively instrument memory stores

Page 23: CRCount: Pointer Invalidation with Reference Counting to

Compiler plugin

val destval type dest typeLLVM IR: some_ptr

Page 24: CRCount: Pointer Invalidation with Reference Counting to

Compiler plugin

val destval type dest type

C code:

LLVM IR: some_ptr

some_ptr

Page 25: CRCount: Pointer Invalidation with Reference Counting to

Compiler plugin

C code:

LLVM IR:val

Not ptr type?

Not ptr type?

Page 26: CRCount: Pointer Invalidation with Reference Counting to

Compiler plugin

C code:

LLVM IR:

val

Ptr type!

Ptr type!

Page 27: CRCount: Pointer Invalidation with Reference Counting to

C Reference counting challenges

• How to know where the pointers are stored

• How to find right instrumentation points• Instrumenting only store instructions that have to do

with the pointers

Page 28: CRCount: Pointer Invalidation with Reference Counting to

Performance Evaluation

• Intel Xeon, 10 cores @ 2.2GHz, 64GB Memory

• Compared

• CRCount (This work)

• DangSan (EuroSys`17)

• Oscar (Security `17)

• Boehm-Demers-Weiser Garbage Collector (latest version)• Only for some benchmarks

• Benchmarks

• SPEC2006 (single-threaded)

• PARSEC (multi-threaded)

• Web servers

Page 29: CRCount: Pointer Invalidation with Reference Counting to

BDW Garbage Collector

• Actively maintained C garbage collector• GC_malloc() + a few other APIs• Automatically frees GC_malloc’ed object when no

references to the object• --enable-redirect-malloc• Redirect malloc() to GC_malloc()

• -DIGNORE_FREE• Ignore free()

• Worked for most of the benchmarks• Some C bench did not work, C++ bench need manual

work

Page 30: CRCount: Pointer Invalidation with Reference Counting to

Runtime overhead (SPEC2006)

GeoMean - CRCount: 22%, DangSan: 44%, Oscar: 41%GeoMean - CRCount: 13.9%, Boehm: 0.7%

Page 31: CRCount: Pointer Invalidation with Reference Counting to

Memory overhead (SPEC2006)

GeoMean - CRCount: 18%, DangSan: 126%, Oscar: 61.5%GeoMean - CRCount: 9.7%, Boehm: 126%

Page 32: CRCount: Pointer Invalidation with Reference Counting to

Runtime overhead (PARSEC)

GeoMean - CRCount: 6.1 ~ 22.4%, DangSan: 6.3 ~ 17.0%GeoMean - CRCount: 4.9 ~ 28.6%, Boehm: 5.3 ~ 38.9%

Page 33: CRCount: Pointer Invalidation with Reference Counting to

Memory overhead (PARSEC)

GeoMean - CRCount: 9.2 ~ 11.6%, DangSan: 45.0 ~ 52.7%GeoMean - CRCount: 5.4 ~ 6.0%, Boehm: 56.6 ~ 70.9%

Page 34: CRCount: Pointer Invalidation with Reference Counting to

Quarantine zone size

Usually small

Page 35: CRCount: Pointer Invalidation with Reference Counting to

Memory leak

• Due to failure to track when pointers are killed• Can be critical for long-running

software•è Run light-weight GC when

new objects in the quarantine reaches certain threshold• Use pointer-bitmap for pointer

locations

• For 256MB, only 0.4% slowdown (for gcc)

Page 36: CRCount: Pointer Invalidation with Reference Counting to

Security eval

• CRCount only delays memory reuse• Attacks through the dangling pointer silently prevented

• Implemented CRCount-det• To detect dangling pointer dereference

• Memory reuse successfully delayed in all cases

Page 37: CRCount: Pointer Invalidation with Reference Counting to

Limitations

• Custom allocator• Should manually insert crc_free before custom free()

• Pointer alignment• cannot track pointers not aligned to 8B boundary è rare

• Limitations in analysis

foo((int)ptr);

void foo(int a) {… = a;

}

struct bar {int x;} bar1;

bar1.x = (int)ptr;…bar2.x = bar1.x;

Page 38: CRCount: Pointer Invalidation with Reference Counting to

Conclusion

• Hard to mitigate UAF efficiently

• Our approach - CRCount• Maintain light-weight data structures (Ref. cnts)• Minimize instrumentation points

• Efficient compared to existing work• 22% runtime, 18% memory overhead on SPEC2006

Page 39: CRCount: Pointer Invalidation with Reference Counting to

Thank you for listening!