SoK: Sanitizing for Security Dokyung Song , Julian Lettner, Prabhu - - PowerPoint PPT Presentation

sok sanitizing for security
SMART_READER_LITE
LIVE PREVIEW

SoK: Sanitizing for Security Dokyung Song , Julian Lettner, Prabhu - - PowerPoint PPT Presentation

SoK: Sanitizing for Security Dokyung Song , Julian Lettner, Prabhu Rajasekaran, Yeoul Na, Stijn Volckaert, Per Larsen, Michael Franz Finding Bugs in C/C++ Manual Analysis Static Analysis Dynamic Analysis AddressSanitizer MemorySanitizer Code


slide-1
SLIDE 1

SoK: Sanitizing for Security

Dokyung Song, Julian Lettner, Prabhu Rajasekaran, Yeoul Na, Stijn Volckaert, Per Larsen, Michael Franz

slide-2
SLIDE 2

Finding Bugs in C/C++

May 2019 2

C/C++ Source Code Code Review/Auditing

Static Analysis

Clang Static Analyzer

Dynamic Analysis Manual Analysis

american fuzzy lop Hand-written test suite libFuzzer Program Inputs

AddressSanitizer MemorySanitizer

slide-3
SLIDE 3

Finding Bugs in C/C++

May 2019 3

C/C++ Source Code Code Review/Auditing

Static Analysis

Clang Static Analyzer

Dynamic Analysis Manual Analysis

american fuzzy lop

AddressSanitizer MemorySanitizer

Hand-written test suite libFuzzer Program Inputs

slide-4
SLIDE 4

Dynamic Analysis Tools for C/C++

  • More than 35 years of research in Dynamic Analysis Tools – often-called “Sanitizers”

– that find vulnerabilities specific to C/C++

May 2019 4

Bcc Memcheck

  • Dr. Memory

LBC ASan Electric Fence PageHeap D&A Safe-C P&F MSCC SoftBounds+CETS CRED BBC EffectiveSan PAriCheck Low-Fat DangSan DangNull 1990 2000 2005 2010 2015 RTCC 2019 TypeSan TySan CaVer CUP SGXBounds FreeSentry MSan Oscar CRCount UBSan 1995 HexType Purify 1980 Undangle

slide-5
SLIDE 5

Exploit Mitigation vs. Sanitization (1/2)

May 2019 5

Attack Flow

Function Pointer Overwrite Indirect Call Heap Overflow Integer Overflow +

slide-6
SLIDE 6

Exploit Mitigation vs. Sanitization (1/2)

May 2019 6

Attack Flow

Function Pointer Overwrite Indirect Call Heap Overflow Integer Overflow +

Exploit Mitigation Security Policies

Control-Flow Integrity Memory Safety Code Pointer Integrity AddressSanitizer UndefinedBehaviorSanitizer … and many others

Sanitization Policies

slide-7
SLIDE 7

Exploit Mitigation vs. Sanitization (1/2)

May 2019 7

Attack Flow

Function Pointer Overwrite Indirect Call Heap Overflow Integer Overflow +

Exploit Mitigation Security Policies

Control-Flow Integrity Memory Safety Code Pointer Integrity

Sanitization Policies

AddressSanitizer UndefinedBehaviorSanitizer … and many others

slide-8
SLIDE 8

Exploit Mitigation vs. Sanitization (2/2)

May 2019 8

Exploit Mitigation Sanitization

The goal is to … Mitigate attacks Find vulnerabilities Used in … Production Pre-release Performance budget is … Very limited Much higher Policy violation leads to … Program termination Problem diagnosis Violations triggered at location of bug Sometimes Always Tolerance for FPs is … Zero Somewhat higher Surviving benign errors is … Desired Not desired

slide-9
SLIDE 9

Undefined Behavior in C/C++

  • Buffer overflow
  • Use-after-free
  • Type errors
  • Format string bug
  • Signed integer overflow
  • Null pointer dereferences
  • etc.

9 May 2019

J.2 Undefined behavior The behavior is undefined in the following circumstances: … — Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.6). … — An object is referred to outside of its lifetime (6.2.4). … — A pointer is used to call a function whose type is not compatible with the referenced type. … — An object has its stored value accessed other than by an lvalue

  • f an allowable type (6.5).

slide-10
SLIDE 10

Undefined Behavior in C/C++

  • Buffer overflow
  • Use-after-free
  • Type errors
  • Format string bug
  • Signed integer overflow
  • Null pointer dereferences
  • etc.

10 May 2019

J.2 Undefined behavior The behavior is undefined in the following circumstances: … — Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.6). … — An object is referred to outside of its lifetime (6.2.4). … — A pointer is used to call a function whose type is not compatible with the referenced type. … — An object has its stored value accessed other than by an lvalue

  • f an allowable type (6.5).

→ Well-known Security Vulnerabilities

slide-11
SLIDE 11

mov rsi, QWORDPTR[rdi+8]

Security Implications of Undefined Behavior in C/C++ (1/2)

May 2019 11

Compile Source Code Binary Code sk = tun->sk; Null-pointer Dereference Null-pointer Dereference tun=NULL

  • 1. Memory and type safety violations vulnerable to memory exploits
slide-12
SLIDE 12

if (!tun) return POLLERR;

// privileged code

Security Implications of Undefined Behavior in C/C++ (2/2)

May 2019 12

?

Compile sk = tun->sk; Source Code Binary Code Null-pointer Dereference tun=NULL

  • 2. Compilation of a program having UBs may result in vulnerable code
slide-13
SLIDE 13

if (!tun) return POLLERR;

// privileged code

Security Implications of Undefined Behavior in C/C++ (2/2)

May 2019 13

?

Compile sk = tun->sk; Source Code Binary Code Null-pointer Dereference tun=NULL

  • 2. Compilation of a program having UBs may result in vulnerable code

Null pointer check gets eliminated (akin to CVE-2009-1897)

slide-14
SLIDE 14

Compile if (!tun) return POLLERR;

// privileged code

mov rsi, QWORDPTR[rdi+8]

Security Implications of Undefined Behavior in C/C++ (2/2)

May 2019 14

sk = tun->sk;

// privileged code

Privilege Escalation Source Code Binary Code

Null pointer check gets eliminated (akin to CVE-2009-1897)

Null-pointer Dereference tun=NULL

  • 2. Compilation of a program having UBs may result in vulnerable code
slide-15
SLIDE 15

Low-Level Vulnerabilities in C/C++ (1/2)

15

Spatial Memory Safety Violation Temporal Memory Safety Violation Use of Uninitialized Variables Pointer Type Errors Bad Casting Other Pointer Type Errors Variadic Function Misuse Other Vulnerabilities Integer Overflow Other UBs

May 2019

  • Most of these vulnerabilities can manifest as memory and type safety

violations.

slide-16
SLIDE 16

Spatial Memory Safety Violation Temporal Memory Safety Violation Use of Uninitialized Variables Pointer Type Errors Bad Casting Other Pointer Type Errors Variadic Function Misuse Other Vulnerabilities Integer Overflow Other UBs

Low-Level Vulnerabilities in C/C++ (2/2)

May 2019 16

  • Some UBs may lead to unsafe code generation today.
  • And, things can change as compiler optimizations evolve – called time-

bombs*.

*

  • W. Dietz, P. Li, J. Regher, and V. Adve; “Understanding integer overflow in C/C++.” In ICSE, 2012.
slide-17
SLIDE 17

Spatial Memory Safety Violation Temporal Memory Safety Violation Use of Uninitialized Variables Pointer Type Errors Bad Casting Other Pointer Type Errors Variadic Function Misuse Other Vulnerabilities Integer Overflow Other UBs

Low-Level Vulnerabilities in C/C++ (2/2)

May 2019 17

  • Some UBs may lead to unsafe code generation today.
  • And, things can change as compiler optimizations evolve – called time-

bombs*.

*

  • W. Dietz, P. Li, J. Regher, and V. Adve; “Understanding integer overflow in C/C++.” In ICSE, 2012.
  • fno-delete-null-

pointer-checks

  • ftrivial-auto-

var-init=zero

  • fno-strict-

aliasing

  • fwrapv
  • ftrapv
slide-18
SLIDE 18

Sanitizer Design and Implementation

May 2019 18

Bug Finding Technique Metadata Management Program Instrumentation

Spatial Memory Safety Violation Red-zone Insertion (Guard Pages) Per-pointer Bounds Tracking Per-object Bounds Tracking Temporal Memory Safety Violation Reuse Delay Lock-and-key Dangling Pointer Tagging Use of Uninitialized Variables Uninitialized Memory Read Detection Uninitialized Value Use Detection Pointer Type Errors Pointer Casting Monitor Pointer Use Monitor Variadic Function Misuse Dangerous Format String Detection Argument Mismatch Detection Other Vulnerabilities Stateless Monitoring

(a) Dynamic Metadata (b) Static Metadata Object … Pointer …

Embedded Disjoint

(a) Language-level Instrumentation (b) IR-level Instrumentation (c) Binary Instrumentation

check(); call check

store ptr 111011101010 101010110101 010101011010 *ptr = 3;

Others Instruction

slide-19
SLIDE 19

Sanitizer Design and Implementation: Bug Finding Techniques

19

Spatial Memory Safety Violation

Red-zone Insertion (Guard Pages) Per-pointer Bounds Tracking Per-object Bounds Tracking

Temporal Memory Safety Violation

Reuse Delay Lock-and-key Dangling Pointer Tagging

Use of Uninitialized Variables

Uninitialized Memory Read Detection Uninitialized Value Use Detection

Pointer Type Errors

Pointer Casting Monitor Pointer Use Monitor

Variadic Function Misuse

Dangerous Format String Detection Argument Mismatch Detection

Other Vulnerabilities

Stateless Monitoring

May 2019

Class of Bugs Bug Finding Technique

Bug Finding Techniques

slide-20
SLIDE 20

store ptr *ptr = 3; May 2019 20

Source code (C/C++) Intermediate Representation (e.g., LLVM IR) Binary

(a) Language-level (b) IR-level (c) Binary-level

check();

call check

11101110101 01010101101 01010101011 0101

Inlined Reference Monitor:

Fine-grained run-time monitoring

  • f program behavior to detect

bugs as they occur.

Compiler Frontend Compiler Backend

(d) Library Interposition

LD_PRELOAD for instrumenting only calls to dynamically-linked external library functions

External Libraries

Sanitizer Design and Implementation: Program Instrumentation Program Instrumentation

call

slide-21
SLIDE 21

(a) Dynamic Metadata (b) Static Metadata

Others Instruction Object Pointer

Sanitizer Design and Implementation: Metadata Management

May 2019 21

Embedded Disjoint

  • Result type of a casting
  • peration
  • Function type used in an

indirect/variadic call

  • Embedded

before, after, and within an

  • bject
  • Fat pointer
  • Tagged pointer
  • Two-level trie
  • Custom
  • Direct-mapped
  • Multi-level
  • Hash table
  • Class hierarchy
  • Type aliasing information

Original program Sanitizer-instrumented program Needs to be created and propagated at run time

Metadata Management

slide-22
SLIDE 22

Sanitizer Design and Implementation: Precision and Overheads

May 2019 22

Bug Finding Technique Metadata Management Program Instrumentation

Spatial Memory Safety Violation Red-zone Insertion (Guard Pages) Per-pointer Bounds Tracking Per-object Bounds Tracking Temporal Memory Safety Violation Reuse Delay Lock-and-key Dangling Pointer Tagging Use of Uninitialized Variables Uninitialized Memory Read Detection Uninitialized Value Use Detection Pointer Type Errors Pointer Casting Monitor Pointer Use Monitor Variadic Function Misuse Dangerous Format String Detection Argument Mismatch Detection Other Vulnerabilities Stateless Monitoring

(a) Dynamic Metadata (b) Static Metadata Object … Pointer …

Embedded Disjoint

(a) Language-level Instrumentation (b) IR-level Instrumentation (c) Binary Instrumentation

check(); call check store ptr 1110111010101 0101011010101 0101011010 *ptr = 3;

Others Instruction

False positives True negatives False negatives True positives

Performance and Memory Overheads Bug Detection Precision and Compatibility

slide-23
SLIDE 23

Our Analysis of Sanitizers

  • Our analysis of 37 tools
  • We benchmarked 10 publicly available

sanitizers on the same experimental platform ( https://github.com/securesystemslab/ sanitizing-for-security-benchmarks)

  • Main observations
  • Performance is not a primary concern
  • Many false positives (marked as ) in tools
  • ther than widely-used ones such as ASan
  • Most of tools only have partial coverage of

bugs ( )

  • Widely deployed tools such as ASan have

even smaller coverage

May 2019 23

slide-24
SLIDE 24

Precision: False Positives and False Negatives

May 2019 24

Well-defined programs w.r.t. the ISO Standard

Programs conforming to the ISO standard

slide-25
SLIDE 25

Precision: False Positives and False Negatives

May 2019 25

Well-defined programs w.r.t. the ISO Standard

Programs conforming to the ISO standard

slide-26
SLIDE 26

Precision: False Positives and False Negatives

May 2019 26

Well-defined programs w.r.t. the ISO Standard

Programs conforming to the ISO standard

e.g., programs creating OOB pointers Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object.*

* ISO/IEC JTC1/SC22/WG14. ISO/IEC 9899:2011, Programming Languages — C

slide-27
SLIDE 27

Precision: False Positives and False Negatives

May 2019 27 *

  • K. Memarian, J. Matthiesen, J. Lingard, K. Nienhuis, D. Chisnall, R. N. M. Watson, and P. Sewell. Into the Depths of C: Elaborating the De Facto Standards. In PLDI’16

But in practice it seems to be common to transiently construct out-of-bounds pointers.* e.g., programs creating OOB pointers

Programs conforming to the de facto standard Programs conforming to the ISO standard

Real-world programs (Or De Facto Standard)

slide-28
SLIDE 28

Precision: False Positives and False Negatives

May 2019 28

Programs disallowed by the policy A typical sanitizer policy

False positives True negatives False negatives True positives

Programs conforming to the de facto standard Programs conforming to the ISO standard

slide-29
SLIDE 29

Reducing Precision Gaps (1/2): Standard Compatibility

May 2019 29

Programs disallowed by the policy A typical sanitizer policy

Compatibility with the ISO and de facto standards

Programs conforming to the de facto standard Programs conforming to the ISO standard

slide-30
SLIDE 30

Reducing Precision Gaps (1/2): Standard Compatibility

  • Many programs transiently construct OOB pointers (de facto standards)
  • Thus, supporting this code idiom increases a tool’s applicability
  • Many tools, however, do not permit transient construction of OOB pointers
  • Some bounds checking tools invalidate pointers as soon as they go OOB
  • Dangling pointer tagging tools may incorrectly invalidate pointers, if OOB pointers are transiently

stored in memory

  • Many tools only support ISO standard compatibility by adding one byte between
  • bjects – this is not enough in practice

May 2019 30

slide-31
SLIDE 31

Reducing Precision Gaps (2/2): Finding Elusive Bugs

May 2019 31

Programs disallowed by the policy A typical sanitizer policy Programs conforming to the de facto standard Programs conforming to the ISO standard

Finding bugs that elude existing or widely-deployed sanitizers

slide-32
SLIDE 32

Reducing Precision Gaps (2/2): Finding Elusive Bugs

  • Subclasses of memory safety violations

that elude AddressSanitizer:

  • Intra-object buffer overflow
  • Buffer overflow into a valid but unintended
  • bject
  • Uses of freed memory that are being

reused

  • Type errors beyond bad casting

(static_casts)

  • C programs or C++ programs using C-style

casts and reinterpret_casts

  • Type errors are UBs that may silently break

programs if does not instruct the compiler using flags like -fno-strict- aliasing

May 2019 32

slide-33
SLIDE 33

Reducing Precision Gaps (2/2): Finding Elusive Bugs

  • Finding these elusive bugs, in general, requires more precise dynamic metadata

tracking

  • Tracking per-pointer metadata such as pointer bounds
  • Tracking effective types of object storage in memory
  • However, such metadata tracking poses precision and performance challenges
  • C’s weak type safety (e.g., pointer to integer casts, uses of void pointers) makes

pointer metadata tracking difficult

  • The C standard has complex effective type and aliasing rules
  • More research is needed in developing sanitizers that can find these elusive bugs

while maintaining good compatibility

May 2019 33

slide-34
SLIDE 34

fatptr=fat(ptr);

Pointer Metadata Tracking Challenges: Uninstrumented Code

May 2019 34

Instrumented Uninstrumented

Fat pointer is not compatible with uninstrumented code Disjoint pointer metadata can get outdated, when uninstrumented code updates a pointer without updating corresponding metadata

Uninstrumented *fatptr *mem=new_ptr; check_bnds(ptr); Instrumented ptr=*mem; *ptr;

slide-35
SLIDE 35

Pointer Metadata Tracking Challenges: Pointer to Integer Casts

  • Even with full instrumentation (via, e.g., dynamic binary translation), sanitizers can

break programs having pointer to integer casts

  • Incompatible with fat/tagged pointers
  • Disjoint pointer metadata can be a choice, but full pointer flow tracking across casts

between pointers and integers can be expensive

  • Existing tools stop tracking pointer metadata once they are cast to integers

May 2019 35

some_object_type * → uint64_t

slide-36
SLIDE 36

Pointer Metadata Tracking Challenges: Multi-threaded Programs

  • Race-free programs that use atomic operations can be problematic
  • Concurrent atomic operations from different threads without putting corresponding metadata
  • perations into the same atomic unit can make metadata go out-of-sync
  • Example of naïve instrumentation:

May 2019 36

atomic_store(addr_of_ptr, ptrA); atomic_store(addr_of_ptr, ptrB); *metadata_of_ptr = metadata_of_ptrA; *metadata_of_ptr = metadata_of_ptrB; Thread A Thread B Instrumented code metadata for ptr out-of-sync! ❶ ❷ ❸ ❹

slide-37
SLIDE 37

Type Error Detection Challenges

  • Rules about determining an effective type of a stored value (i.e., effective type rules)

are complex, due to weakly-typed nature of C

  • Prevalent uses of void pointer type and (omnipotent) character pointer type
  • malloc returns void *
  • memcpy-family of functions take and return void *
  • Type punning through C-style casts and union
  • There are some tools that implement over-approximations of effective type rules, but

precision and performance trade-offs are yet to be explored.

  • Also, type error checking itself can be costly, because C’s aliasing rules permit a

stored value to be accessed by using pointers of many different types

May 2019 37

slide-38
SLIDE 38

Other Future Research Directions

Composing sanitizers

  • Can find bugs closer to their

source without generating duplicated bug reports for the bug’s side-effects

Using hardware features to improve performance and compatibility

  • e.g., Pointer tagging/memory

tagging support in HW

Kernel and bare metal support

  • Sanitizers for OS kernels, or

non-user-space programs in general

May 2019 38

slide-39
SLIDE 39

Q & A Thank you!

Dokyung Song Ph.D. Student at UC Irvine dokyungs@uci.edu

39 May 2019