Maggi Virtualization Talk 2014
Maggi Virtualization Talk 2014
VIRTUALIZATION
(ISSISP 2014 tutorial)
Federico Maggi — @phretor
[email protected]
BASIC CONCEPTS
What is Virtualization?
Virtualization is a very similar concept to emulation
Emulation: the system pretends to be another system
e.g., “executing” ARM instructions using a program compiled for and
running on a x86 processor
Virtualization: the system pretends to be one or more systems
e.g., multiple operating systems sharing the same CPU
mov %ebx,0x4(%ebp)
dyngen
code
generator
caching
Micro Operations
# movl_T0_r1
# ebx = env->regs[1]
mov 0x4(%ebp),%ebx
# addl_T0_im -16
# ebx = ebx - 16
add $0xfffffff0,%ebx
# movl_r1_T0
# env->regs[1] = ebx
mov %ebx,0x4(%ebp)
Back to Virtualization
Host OS
Host
Machine
Virtualizing a Machine
CPU virtualization
how to run instructions “concurrently”?
Memory virtualization
how to isolate each guest OS in a physical memory space?
Resource management
how to create, run, destroy, migrate guests?
unused
applications
kernel
Privileged instructions
may execute in a privileged mode (ring 0),
but will trap if executed outside this mode (ring >0).
Control-sensitive instructions
attempt to change the configuration of resources in the system
e.g., physical memory assigned to a program.
Behavior-sensitive instructions
behave in a different way depending on the configuration of
resources
e.g., load and store operations that act on virtual memory
Related to so-called “side effects”.
CPU Virtualization: Requirements
All sensitive instructions must also be privileged
instructions
so that they trap if executed in non-privileged mode
Why? A hypervisor must be able to intercept any
instructions that change the state of the machine in a
way that impacts other processes.
The job of the hypervisor is to keep track of the state of
the CPU, and expose a different state to each OS
when switching between two OSs, we need to save the state
of the CPU exposed to the first OS, and restore the state for the
new one. And so on.
CPU Virtualization: Requirements
OS1 OS2
hypervisor
it's a trap!
cpu
Problems of x86 Virtualization
There is a set of 17 instructions in the x86 instruction set that
does not have this property.
Sensitive register instructions
Example: the LAR and LSL instructions load information
about a specified memory segment. Because these
cannot be trapped, there is no way for the hypervisor
to rearrange the memory layout without a guest OS
finding out.
Protection system instructions
Example: SIDT, set the values of certain condition
registers, but have no corresponding load
instructions.
So, every time they execute they must be trapped and
the new value stored elsewhere as well, so it can be
restored when the virtual machine is re-activated.
Some Solutions for x86 Virtualization
Guest OS
Guest CPU PC
VMware
VMware's Translation Example
Translation step
isPrime(49)
Translation step
Performance Issues
Hardware
Paravirtualization (Xen Approach)
Except for the problematic instructions, x86 is virtualizable
Ring 1 is unused: lets use it!
Paravirtualization (Xen Approach)
Approach: ignore the problematic instructions and let the
OS deal with them (i.e., inform the hypervisor).
If a guest system executes an instruction that doesn’t
trap while inside a paravirtualized environment, the
guest has to deal with the consequences.
Conceptually, this is similar to the binary rewriting
approach, except that here the rewriting happens at
compile time (or design time), rather than at runtime.
The OS is designed with support for running in ring 0, but
it runs in ring 1 now
it means that it cannot execute privileged instructions,
because the will simply fail
Privileged Instructions via Ring 1
In order to simulate a privileged instruction the
hypervisor exposes a set of hypercalls.
A hypercall is conceptually similar to a system call. On
UNIX systems, the convention for invoking a system call
is to push the values and then raise an interrupt.
A regular system call from ring 3 (process) to ring 0 (OS)
push dword 0 # push parameters
mov eax, 1 # set the system call
push eax # push syscall identifier
int 80h # raise software interrupt
int 80h
Physical
memory
Control
register
Per process
x86 Problems and Solutions
unused
applications
kernel
hypervisor
Hardware-Assisted Virtualization
PT1 PT2
hypervisor hypervisor
CPU CPU
vRAM1 vRAM2
RAM
OTHER FORMS OFVIRTUALIZATION
(examples)
Other Forms of Virtualization (1/3)
ABI – Application Binary Interface Virtualization
allow the execution of binary formats on other OSs
e.g., Wine: run Windows PE binaries on Linux
wrapper around Linux system calls to emulate Windows API
Windows PE
Native application
Native Application Binary Interface (e.g., ELF)
Kernel (e.g., Linux)
Hardware
Other Forms of Virtualization (2/3)
Kernel
Filesystem
Storage Hardware
Other Forms of Virtualization (3/3)
Language based
define an intermediate target language, often called bytecode, for
compiled applications
usually closer to the source code than to the machine code
define a semantic and virtual machine for executing the
intermediate language
allow complete isolation at the “instruction” level
each application runs in a small virtual machine, which is actually
a process in the guest operating system
allow program portability across different guests, at the cost of
porting the virtual machine
popular examples: Java, Dalvik (Android), .NET
APPLICATION OF VIRTUALIZATION
(example)
Using Virtualization for Dynamic
Analysis of Unknown Binaries
Problem
x86 executable binary that does something probably bad to our
system.
we don't know what it does, and have no time to manually
analyze it (as there are ~180 million suspicious binaries out
there)
Approach
let it run on a real machine and observe what it does
interesting actions: system calls, because they imply that
privileged code is executed
open a network connection, write to a file, place a call, send SMS
Example Process Trace (Mac OS X)
Unmodified guest
QEMU-based
device emulator
Introspection
interface
Semantic
reconstruction
Semantic Gap Problem
The instruction trace is not very informative
movl $0x200005d, %eax
syscall
jae 0x7fff988ac9b4
jmpq 0x7fff988a919a
fork( ) = 0x125
getpgid(0x41) = 0x23
setpgid(0x125, 0x23) = 0x0
getuid32( ) = 0x0
open(“/acct/uid/0/tasks”, …) = ...
fstat64(0x13, 0xbef7f910) = 0x0
mprotect(0x40008000, 0x1000, 0x3) = 0x0
Conclusive Remarks
Bovet and Cesati, “Understanding the Linux Kernel”, 3rd edition, O'Reilly,
2006.
Reina et al., “A System Call-Centric Analysis and Stimulation Technique to
Automatically Reconstruct Android Malware Behaviors”, Procs. of EuroSec
2013. Tool available online at http://copperdroid.isg.rhul.ac.uk/
Matthew Portnoy, “Virtualization Essentials”, Wiley, 2012