Ouroboros:Tearing Xen Hypervisor With the Snake
Augeust 04, 2016
Shangcong Luan
Alibaba Cloud Platform Security Team1

Who am I?

2014 - 2015 at Vulnhunt Security Team for APT Defense
2015 - nowx at Alibaba Cloud for Cloud Security
2016.05xxxx Advanced Exploitation: Xen Hypervisor VM Escape, HITB AMS 2016

2Agenda

1. Introduction
2. XSA-182/CVE-2016-6258
3. Exploitation Technologies
4. The End

3

Introduction

The Xen Project

The Xen ProjectTM is the leading open source virtualization platformthat is powering some of the largest clouds in production today.
from xenproject.org4Xen Architecture5

Xen Hypervisor• CPU Scheduling• Memory Management• VM Executation
• ...

6

Xen Domain

Dom0:• Privileged Domain• Control Other Domains
domU:• Dom1, Dom2, Dom3 ...
• Unprivileged Domains

7

Xen Domains Running Mode

PVM:
• paravirtualization machine• modified OS kernel
HVM:• hardware-assisted virtualization machine• unmodified OS kernel• CPU/MMU =¿ hardware assistance8

Xen Memory Managementx86 Paravirtualised Memory Management:
Direct Paging

9

Direct Paging: Address Spaces

guest pseudo-physical frame number (gpfn)
equals tomachine frame number (mfn)

10Direct Paging: Page Typemutually-exclusive page types:• PGT writable page could be writable mapped into Guest• PGT l1 page table L1 page table type
• PGT l2 page table L2 page table type
• PGT l3 page table L3 page table type
• PGT l4 page table L4 page table type11

Direct Paging: Safe Invariants

1. A guest OS may always create readable mappings to its own page frames,
regardless of their current types.
2. A frame may only safely be retasked when its reference count is zero.
PV Guest Cannot Read/Write Security-Sensitive Memorys,e.g., page tables.

12

Direct Paging: Safe Invariants

1. A guest OS may always create readable mappings to its own page frames,
regardless of their current types.
2. A frame may only safely be retasked when its reference count is zero.
PV Guest Cannot Read/Write Security-Sensitive Memorys,e.g., page tables.

13

Direct Paging: Safe Invariants

1. A guest OS may always create readable mappings to its own page frames,
regardless of their current types.
2. A frame may only safely be retasked when its reference count is zero.
PV Guest Cannot Read/Write Security-Sensitive Memorys,e.g., page tables.

14

XSA-182/CVE-2016-6258

Timeline

15

Xen Security Advisoriy1

116
http://xenbits.xen.org/xsa/advisory-182.html

mmu update hypercall request

17

mod l2 entry()18

mod l2 entry(): page type check19mod l2 entry(): page type check20

translation 1/4

21

translation 2/4

22

translation 2/423

mod l2 entry(): fast-path update check24mod l2 entry(): fast-path update check25

translation 3/426

translation 4/427

Whole Memory Access28

Arbitrary Machine Memory Read/Writefour steps translate the XSA-182 toArbitrary Machine Memory Read/Write

29

Exploitation Technologies

Expectation• Arbitrary Code Execution Within Domain Context• get dom0 root shell• get other domains root shell• Arbitrary Code Execution Within VMM Context• fake hypercall inject30

Expectation: get dom0 root shell

Hijack Vectors:• VDSO/vsyscall Page (Linux)• SharedUserData Page (Windows)• Hypercall Page
• ...

31

Hypercall Page Hijack

Hypercall Page
• A 4K page alloced by Guest Kernel filled with 0xCC
• Need to be initialized with hypercall stub codes• Guest Kernel use it to do hypercall request• Each Guest Kernel only has one hypercall page

32

Hypercall Page Stub Codes

33

Hypercall Page Signature

34

Hypercall Page Layout

35

Hypercall Page Hijacked Layout

36

Hypercall Page Hijacked Layout

37

Expectation: get dom0 root shell• Arbitrary Code Execution Within Domain Context• get dom0 root shell• get other domains root shell• Arbitrary Code Execution Within VMM Context• fake hypercall

38

Expectation: get other domains root shell• Arbitrary Code Execution Within Domain Context• get dom0 root shell• get other domains root shell• Arbitrary Code Execution Within VMM Context• fake hypercall

39

Expectation: VMM code inject• Arbitrary Code Execution Within Domain Context• get dom0 root shell• get other domains root shell• Arbitrary Code Execution Within VMM Context• fake hypercall

40

hypercall table41

hypercall args table42

hypercall args table43

fake hypercall steps
Step 1: allocate a memory area and deploy codes in it:
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
44

fake hypercall steps
Step 1: allocate a memory area and deploy code in it
Step 2: search hypercall table and modify slot N to refer to the memory area
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding
45fake hypercall steps
Step 1: allocate a memory area and deploy code in it
Step 2: search hypercall table and modify slot N to refer to the memory area
Step 3: bypass SMEP and SMAP features:
padding set PTE.U/S = 0
padding padding
padding padding
padding padding
padding padding
padding padding
padding padding46

fake hypercall steps
Step 1: allocate a memory area and deploy code in it
Step 2: search hypercall table and modify slot N to refer to the memory area
Step 3: bypass SMEP and SMAP features
Step 4: request this hypercall:padding MOV $N, %RAX
padding SYSCALL
padding padding
padding padding
padding padding
padding padding
47

Expectation• Arbitrary Code Execution Within Domain Context• get dom0 root shell• get other domains root shell• Arbitrary Code Execution Within VMM Context• fake hypercall

48

The End10 years old$ git show f87f8a7110e5dd57091b8484685953414693e2a3
Date :+++++++

Tue Feb 8 15:13:45 2005 +0000

if ( l2_p gentry_v al ( nl2e ) & _PAGE_PRESENT ){/* Differ in mapping ( bits 12 -31) or presence ( bit 0)? */if ( (( l 2_pgentry_val ( ol2e ) ^ l2_pgentry_val ( nl2e )) & ~0 xffe ) == 0 )return update_l2e ( pl2e , ol2e , nl2e );Thanks!

Question?

Shangcong Luan from Alibaba Cloud Platform Security Team49