Top Banner
1 Xenpwn – Breaking Paravirtualized Devices Felix Wilhelm
52

Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

Oct 18, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

1

Xenpwn – Breaking Paravirtualized DevicesFelix Wilhelm

Page 2: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

2

#whoamio Security Researcher @ ERNW Researcho Application and Virtualization Securityo Recent Research

o Security Appliances (Palo Alto, FireEye)o Hypervisors

o @_fel1x on twitter

Page 3: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

3

Agendao Device Virtualization & Paravirtualized Deviceso Double Fetch Vulnerabilitieso Xenpwn: Architecture and Designo Resultso Case Study: Exploiting xen-pciback

Page 4: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

4

Device Virtualizationo Virtualized systems need access to virtual

deviceso Disk, Network, Serial, ...

o Traditionally: Device emulationo Emulate old and well supported hardware

deviceso Guest OS does not need special driverso Installation with standard installation sources

supported

Page 5: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

5

Paravirtualized Deviceso Most important downsides of emulated devices:

o Hard to implement securely and correctlyo Slow performanceo No support for advanced features

o Solution: Paravirtualized Deviceso Specialized device drivers for use in virtualized systemso Idea: Emulated devices are only used as fallback mechanism o Used by all major hypervisorso Not the same as Xen paravirtualized domains!

Page 6: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

6

Paravirtualized Deviceso Split Driver Model

o Frontend runs in Guest systemo Backend in Host/Management domain

o Terminology differs between hypervisorso VSC / VSP in Hyper-Vo Virtio devices and drivers

o Implementations are quite similar

Page 7: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

7

Paravirtualized Deviceso PV devices are implemented on top of shared

memoryo Great Performanceo Easy to implemento Zero copy algorithms possible

o Message protocols implemented on topo Xen, Hyper-V and KVM all use ring buffers

o Shared memory mappings can be constant or created on demand

Page 8: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

8

Security of PV Deviceso Backend runs in privileged context è Communication between

frontend and backend is trust boundaryo Low level code + Protocol parsing è Bugso Examples

o Heap based buffer overflow in KVM disk backend (CVE-2011-1750)o Unspecified BO in Hyper-V storage backend (CVE-2015- 2361)

o Not as scrutinized as emulated deviceso Device and hypervisor specific protocolso Harder to fuzz

Page 9: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

9

Very Interesting Targeto Device emulation often done in user space ßà PV backend often

in kernel for higher performanceo Compromise of kernel backend is instant win J

o PV devices are becoming more importanto More device types (USB, PCI pass-through, touch screens, 3D

acceleration)o More features, optimizations

o Future development: Removal of emulated deviceso see Hyper-V Gen2 VMs

Page 10: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

10

Research Goalo ”Efficient vulnerability discovery in Paravirtualized Devices”o Core Idea: No published research on the use of shared memory

in the context of PV deviceso Bug class that only affect shared memory? è Double fetches!

Page 11: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

11

Double Fetch Vulnerabilitieso Special type of TOCTTOU bug affecting shared memory.o Simple definition: Same memory address is accessed multiple

times with validation of the accessed data missing on at least one access

o Can introduce all kinds of vulnerabilitieso Arbitrary Write/Reado Buffer overflowso Direct RIP control J

Page 12: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

12

Double Fetch Vulnerabilitieso Term “double fetch” was coined by Fermin J. Serna in 2008

o But bug class was well known before thato Some interesting research published in 2007/2008

o Usenix 2007 “Exploiting Concurrency Vulnerabilities in System Call Wrappers” - Robert N. M. Watson

o CCC 2007: “From RING 0 to UID 0” and Phrack #64 file 6 – twiz, sgrakkyu

o First example I could find is sendmsg() linux bug reported in 2005o Happy to hear about more J

Page 13: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

13

Example: sendmsg()

Page 14: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

14

Bochspwno “Identifying and Exploiting Windows Kernel

Race Conditions via Memory Access Patterns” (2013)o by j00ru and Gynvael Coldwind

o Uses extended version of Bochs CPU emulator to trace all memory access from kernel to user space.

Page 15: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

15

Bochspwno Resulted in significant number of Windows bugs (and a well

deserved Pwnie)o but not much published follow-up research

o Whitepaper contains detailed analysis on exploitability of double fetcheso On multi core system even extremely short races are exploitable

o Main inspiration for this research.

Page 16: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

16

Example: Bochspwn

Page 17: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

17

Xenpwno Adapt memory access tracing approach used by Bochspwn for

analyzing PV device communication.o Why not simply use Bochspwn?

o Extremely slowo Passive overhead (no targeted tracing)o Compatibility issueso Dumping traces to text files does not scale

o Idea: Implement memory access tracing on top of hardware assisted virtualization

Page 18: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

18

Page 19: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

19

Xenpwn Architectureo Nested virtualization

o Target hypervisor (L1) runs on top of base hypervisor (L0)

o Analysis components run in user space of L1 management domain.o No modification to hypervisor requiredo Bugs in these components do not crash whole

systemo L0 hypervisor is Xen

Page 20: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

20

libVMIo Great library for virtual machine

introspection (VMI)o Hypervisor agnostic (Xen and KVM)o User-space wrapper around hypervisor APIs

o Allows access to and manipulation of guest state (memory, CPU registers)

o Xen version supports memory events

Page 21: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

21

libVMI Memory Eventso Trap on access to a guest physical addresso Implemented on top of Extended Page Tables

(EPT)o Disallow access to GPAo Access triggers EPT violation and VM exito VM exit is forwarded to libvmi handler

Page 22: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

22

Memory Access Tracing withlibvmi

1. Find shared memory pages2. Register memory event handlers3. Analyze memory event, extract needed

information and store in trace storage.4. Run analysis algorithms (can happen much

later)

Page 23: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

23

Trace Collectoro Use libvmi to inspect memory and identify shared memory pages

o Target specific code. o Identify data structures used by PV frontend/backend and addresses

of shared pageso Registers memory event handlerso Main work is done in callback handler

o Disassemble instructions using Capstone

Page 24: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

24

Callback handler

Page 25: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

25

Trace Storageo Storage needs to be fast and persistent

o Minimize tracing overheado Allow for offline analysis

o Nice to have: Efficient compressiono Allows for very long traces

o Tool that fulfills all these requirements: Simutraceo simutrace.org

Page 26: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

26

Simutraceo Open source project by the Operation System

Group at the Karlsruhe Institute of Technologyo Designed for full system memory tracing

o All memory accesses including their contento C++ daemon + client library

o Highly efficient communication over shared memory pages

o Uses specialized compression algorithm optimized for memory traceso High compression rate + high speed

o Highly recommended!

Page 27: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

27

Trace EntriesFor everymemory access: For every unique instruction:

Page 28: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

28

Double FetchAlgorithm

Simplified version

Page 29: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

29

Advantages & Limitationso Good:

o Low passive overheado Largely target independent

o only Trace collector requires adaptiono Easy to extend and develop

o Bado High active overhead

o VM exits are expensiveo Reliance on nested virtualization

Page 30: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

30

Nested Virtualization on Xeno Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered

"tech preview". For many common cases, it should work reliably and with low overhead

o Reality:o Xen on Xen workso KVM on Xen works (most of the time)o Hyper-V on Xen does not work L

Page 31: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

31

Resultso KVM: no security critical double fetches

o Main reason seems to be endian independent memory wrapperso .. but discovered other interesting issues while reading the virtio code

;)o bhyve: one very interesting result

o Ongoing disclosure processo Xen: Three interesting double fetches

Page 32: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

32

QEMU xen_disk

Normally not exploitable thanks to compiler optimizations

Page 33: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

33

xen-blkback

OOB Read/Write

Page 34: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

34

xen-pciback

Page 35: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

35

xen-pciback: xen_pcibk_do_op

Page 36: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

36

xen-pcibacko switch statement is compiled into jump tableo op->cmd == $r13+0x4o Points into shared memoryo Range check and jump use two different

memory accesseso Valid compiler optimization

o op is not marked as volatile

Page 37: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

37

Exploiting pcibacko Race is very small: 2 Instructions

o But can be reliably won if guest VM has multiple cores

o Lost race does not have any negative side effectso Infinite retries possible

o Simple to triggero Send PCI requests while flipping value using

XOR

Page 38: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

38

Exploiting pcibacko Indirect jump è No immediate RIP control

o Need to find reliable offset to function pointero Load address of xen-pciback.ko is randomo Virtual address of backend mapping also not knowno A lot of similarities to a remote kernel exploito Chosen approach: Trigger type confusion to get write primitiv

Page 39: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

39

Type Confusiono Second jump table generated for xen-pciback

o Almost directly behind the jump table generated for vulnerable function

o XenbusStateInitialized uses value of r13 register as first argumento Should be a pointer to a xen_pcibk_device

structureo Is a pointer to the start of the shared memory

page J

Page 40: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

40

Getting a write primitveo xen_pcibk_attach first tries to lock the

dev_lock mutex of referenced structure.o Gives us the possibility to call mutex_lock

with a fake mutex structureo mutex_lock

o Fastpath: Switch lock count from 1 -> 0o Slowpath: Triggered when lock count != 1

Page 41: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

41

Getting a write primitive: mutex_lock slowpath

1. mutex_optimistic_spin needs to fail. o Can be achieved by setting lock->owner to a

readable zero page2. If lock count still not 1, mutex_waiter

structure is created and stored on stack3. mutex_waiter structure is added to lock-

>wait_list and kernel thread goes to sleeptill wake up.

è Pointer to waiter is written to attackercontrolled location.

Page 42: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

42

Write Primitiveo write-where but not write-what

o Pointer to pointer to attacker controlled datao Can‘t simply overwrite function pointers

o One shoto pciback is locked due to xen_pcibk_do_op

never returningo Idea: Add faked entries to a global linked list.

o Requires known kernel version + no KASLR orinfoleak

Page 43: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

43

list_head.next

list_head.prev

controlled data

fake_prev

entry1.next

entry1.prev

entry2.next

entry2.prev

Page 44: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

44

list_head.next

list_head.prev

controlled data

fake_prev

waiter

prev

next

Page 45: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

45

Overwrite targeto Global data structure

o Need to know address of list_heado No new elements should be attached during run time

o list_head.prev is not changed, new entry might be added directlybehind list_head

o Needs to survive one “junk“ entryo No full control over waiter structure / stack frame

Page 46: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

46

Page 47: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

47

fs/exec.c: formatso formats linked list contains entries for different file formats

supported by execo ELFo #! shell scriptso a.out format

o Walked every time exec* syscall is called to load input file.o waiter entry is skipped because try_module_get function fails

Page 48: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

48

Getting Code Executiono Set address of load_binary pointer to stack pivoto ROP chain to allocate executable memory and copy shellcode

o vmalloc_exec + memcpyo Restore original formats listo $shellcodeo Return to user space

Page 49: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

49

Demo

Page 50: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

50

Open Sourceo Xenpwn open source release:

o https://github.com/felixwilhelm/xenpwno Whitepaper contains a lot more technical details

o Implementation detailso Performance evaluationo ...

Page 51: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

51

Future Worko Use Xenpwn against Hyper-V and VMWare

o Requires improved support for nested virtualizationo Identify and analyze other shared memory trust boundaries

o Sandboxes?o What types of bugs can we find with full memory traces?

Page 52: Xenpwn – Breaking Paravirtualized Devices - Black Hat · Nested Virtualizationon Xen o Xen Doku: Nested HVM on Intel CPUs, as of Xen 4.4, is considered "tech preview". For many

52

Thanks for your Attention!

Q&A