An Evil Copy: How the Loader Betrays You Xinyang Ge 1,3 , Mathias - - PowerPoint PPT Presentation

an evil copy how the loader betrays you
SMART_READER_LITE
LIVE PREVIEW

An Evil Copy: How the Loader Betrays You Xinyang Ge 1,3 , Mathias - - PowerPoint PPT Presentation

An Evil Copy: How the Loader Betrays You Xinyang Ge 1,3 , Mathias Payer 2 and Trent Jaeger 3 Microsoft Research 1 Purdue University 2 Penn State University 3 Systems and Internet Infrastructure Security Laboratory (SIIS) Page 1 Problem: A


slide-1
SLIDE 1

Systems and Internet Infrastructure Security Laboratory (SIIS) Page 1

An Evil Copy: How the Loader Betrays You

Xinyang Ge1,3, Mathias Payer2 and Trent Jaeger3 Microsoft Research1 Purdue University2 Penn State University3

slide-2
SLIDE 2

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

2

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; } // test.c const int foo = 10;

slide-3
SLIDE 3

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

2

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; } // test.c const int foo = 10;

slide-4
SLIDE 4

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

  • 1 Executable
  • cc main.c test.c
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc [–fPIE] main.c -L. –ltest

3

slide-5
SLIDE 5

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

  • 1 Executable
  • cc main.c test.c
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc [-fPIE] main.c -L. –ltest

3

slide-6
SLIDE 6

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

  • 1 Executable
  • cc main.c test.c
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc [-fPIE] main.c -L. –ltest

3

…Nothing happened?

slide-7
SLIDE 7

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

  • 1 Executable
  • cc main.c test.c
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc [-fPIE] main.c -L. –ltest
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc –fPIC main.c -L. –ltest

3

…Nothing happened?

slide-8
SLIDE 8

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Problem: A Motivating Example

  • 1 Executable
  • cc main.c test.c
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc [-fPIE] main.c -L. –ltest
  • 1 Executable + 1 Library
  • cc -fPIC –shared test.c –o libtest.so
  • cc –fPIC main.c -L. –ltest

3

…Nothing happened?

slide-9
SLIDE 9

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

What happened so far...

4

non-PIC executable PIC executable local “foo” foreign “foo”

…Nothing happened?

Obviously, foo is not in read-only memory in the above case, but WHY?

slide-10
SLIDE 10

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Building Process

5

compiling linking loading

slide-11
SLIDE 11

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Building Process

5

compiling linking loading

slide-12
SLIDE 12

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

What does “extern” mean

6

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; }

slide-13
SLIDE 13

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

What does “extern” mean

6

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; }

foo is defined in a different file but still in the same image (w/o -fPIC flag) foo is defined in a different file and potentially in a different image (w/ -fPIC flag)

slide-14
SLIDE 14

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

What does “extern” mean

6

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; }

foo is defined in a different file but still in the same image (w/o -fPIC flag) foo is defined in a different file and potentially in a different image (w/ -fPIC flag)

slide-15
SLIDE 15

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in the same image

7

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov $0x64,offset_to_foo(%rip) mov $0x0,%rax pop %rbp ret

slide-16
SLIDE 16

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in the same image

7

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov $0x64,offset_to_foo(%rip) mov $0x0,%rax pop %rbp ret

The compiler assumes foo’s location can be statically determined by the linker, and emits a single MOV instruction to write to foo.

slide-17
SLIDE 17

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in the same image

7

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov $0x64,offset_to_foo(%rip) mov $0x0,%rax pop %rbp ret

The compiler assumes foo’s location can be statically determined by the linker, and emits a single MOV instruction to write to foo.

data GOT code

foo

slide-18
SLIDE 18

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

What does “extern” mean

8

// main.c extern const int foo; int main() { *(int *)&foo = 100; return 0; }

foo is defined in a different file but still in the same image (w/o -fPIC flag) foo is defined in a different file and potentially in a different image (w/ -fPIC flag)

slide-19
SLIDE 19

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in a different image

8

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov offset_to_foo_got(%rip),%rax mov $0x64,(%rax) mov $0x0,%rax pop %rbp ret

slide-20
SLIDE 20

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in a different image

8

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov offset_to_foo_got(%rip),%rax mov $0x64,(%rax) mov $0x0,%rax pop %rbp ret

The compiler assumes foo’s loca;on cannot be sta;cally determined and emits two MOV instruc;ons: one to retrieve foo’s address from its GOT slot, and the other to actually write to foo.

slide-21
SLIDE 21

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

foo is defined in a different image

8

// main.o – assuming same image <main>: push %rbp mov %rsp,%rbp mov offset_to_foo_got(%rip),%rax mov $0x64,(%rax) mov $0x0,%rax pop %rbp ret

The compiler assumes foo’s loca;on cannot be sta;cally determined and emits two MOV instruc;ons: one to retrieve foo’s address from its GOT slot, and the other to write to foo.

data GOT code

foo’s address

slide-22
SLIDE 22

Systems and Internet Infrastructure Security Laboratory (SIIS) Page 9

Without –fPIC flag, GCC and Clang

  • n Linux assumes foo is defined in

the same image.

slide-23
SLIDE 23

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Building Process

10

compiling linking loading

slide-24
SLIDE 24

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

11

Hi, I am the linker. Oops, foo is actually defined in a different

  • image. How can I resolve the

reference to foo? data GOT code

<main>: ... mov $0x64,offset_to_foo(%rip) ...

executable

slide-25
SLIDE 25

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

11

Let me allocate a local copy of foo and have the dynamic loader to relocate the original variable to this new copy. data GOT code

<main>: ... mov $0x64,offset_to_foo(%rip) ...

executable

slide-26
SLIDE 26

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

11

Let me allocate a local copy of foo and have the dynamic loader to relocate the original variable to this new copy. data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable foo = 0

slide-27
SLIDE 27

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Building Process

12

compiling linking loading

slide-28
SLIDE 28

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

13

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 0 foo = 10 address of foo

slide-29
SLIDE 29

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

13

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 0 foo = 10 address of foo

slide-30
SLIDE 30

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

13

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

slide-31
SLIDE 31

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

13

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

slide-32
SLIDE 32

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

13

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

Violation

slide-33
SLIDE 33

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Security Concerns

  • Expose “read-only” data to memory corruption

attacks

  • Making C++ vtables mutable can break existing defenses
  • VTV, Interleaving, SafeDispatch
  • Making format string writable can enable printf-oriented

programming

  • Printf-oriented programming requires mutable format string to

implement branching

  • File names
  • IP addresses
  • ...

14

slide-34
SLIDE 34

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Evaluations

  • Do Copy Relocation Violations occur in practice?
  • Analyze 54,045 packages in Ubuntu 16.04 LTS
  • 34,291 executables + 58,862 dynamic
  • Do Copy Relocation Violations weaken security

mitigations?

  • Evaluate a set of CFI defenses in face of copy relocation

violations

  • Implications on other platforms
  • Windows and macOS

15

slide-35
SLIDE 35

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Real-world Copy Relocation Violations

16

Copy Reloca;on Viola;ons

vtables

  • func. ptrs.

generic ptrs. format str file names generic strs

  • thers
  • 69,098 copy relocation

violations in 6,449 (out of 34,291) executables

  • 28,497 vtables copied to

writable memory in 4,291 executables

  • Among the top 10 most

common copy relocation violations, 8 of them are vtables from libstdc++.so

slide-36
SLIDE 36

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Security Evaluation

  • Developed a small C++ program that has an

intentional vtable corruption vulnerability

  • Run the program under a set of 7 CFI defenses

17

Defenses Check Func Ptr Check VTable Bypassable VTrust VTV vfGuard Interleaving SafeDispatch SafeDispatch2 RockJIT

slide-37
SLIDE 37

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Other Platforms

  • Windows
  • MSVC requires explicit annotation to differentiate “intra-

module extern” from “inter-module extern”

  • The example program cannot be built on Windows
  • macOS
  • The compiler conservatively assumes “extern” is from a

different image

  • The linker uses GOT to serve those references
  • Copy relocations do not exist on macOS

18

slide-38
SLIDE 38

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

macOS issue

  • macOS has its own issue that results in the same

consequence

  • macOS’s compiler allocates data that potentially

requires runtime patching in __DATA__.__const section

  • However, the loader does not reprotect it as read-only at

runtime

  • As a result, read-only data (e.g., vtable) remains writable

19

slide-39
SLIDE 39

Systems and Internet Infrastructure Security Laboratory (SIIS) Page 20

Copy relocation violations seem prevalent in current Linux systems. Then, how can we get rid of them?

slide-40
SLIDE 40

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Mitigations

  • Eliminate copy relocations entirely
  • Recompile executable using -fPIC flag, -fPIE not enough
  • -fPIC flag forces the compiler to treat non-static global variables

as defined in a different image

  • Respect the memory protection while performing copy

relocations

  • Determine the memory protection permission at link time
  • Allocate the variable copy from a section protected by RELRO
  • Both GNU Binutils and LLVM are adopting this approach

21

slide-41
SLIDE 41

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Mitigations

  • Eliminate copy relocations entirely
  • Recompile the executable using -fPIC flag
  • -fPIC flag forces the compiler to treat non-static global variables

as defined in a different module

  • Respect the memory protection while performing copy

relocations

  • Determine the memory protection permission at link time
  • Allocate the variable copy from a section protected by RELRO
  • Both GNU Binutils and LLVM are adopting this approach

22

slide-42
SLIDE 42

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Conclusions

  • Identified a design flaw in the compiler toolchain on Linux
  • Copy relocation can strip the “const” attribute specified by the

programmer

  • Proposed mitigations
  • Eliminate copy relocations entirely
  • Preserve the memory protection of the relocated variables
  • Evaluated Copy Relocation Violations in real world
  • Studied 54,045 packages in Ubuntu 16.04 LTS
  • Copy relocation violations occur commonly in many programs
  • Copy relocation violations can subvert existing defenses

23

slide-43
SLIDE 43

Systems and Internet Infrastructure Security Laboratory (SIIS) Page 24

Questions

slide-44
SLIDE 44

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Variable Type Inference

  • Requirements
  • No source code
  • No debug information
  • Heuristics
  • Pointers:
  • Use relocation information to identify pointers in general
  • Use pointer value to determine code pointer vs data pointer
  • Strings:
  • All bytes are ASCII characters
  • Use ‘/’ to determine file paths and ‘%’ to determine format strings

44

slide-45
SLIDE 45

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

45

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo?

slide-46
SLIDE 46

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

46

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo?

The dynamic loader patches foo’s GOT entry in the library so that it points to the new copy

slide-47
SLIDE 47

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

47

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo?

slide-48
SLIDE 48

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

48

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo? Can the library access foo without the GOT indirec<on?

slide-49
SLIDE 49

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

49

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo? Can the library access foo without the GOT indirec<on?

Mostly it won’t because, by default, libraries treat exported global variables as “external”

slide-50
SLIDE 50

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

50

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo? Can the library access foo without the GOT indirec<on?

slide-51
SLIDE 51

Systems and Internet Infrastructure Security Laboratory (SIIS) Page

Copy Relocation

51

library data GOT code

<main>: ... mov $0x64,0x200970(%rip) ...

executable data rodata code GOT foo = 10 foo = 10 address of foo

What if the library accesses foo? Can the library access foo without the GOT indirec<on?

Violation