Attacking a co-hosted VM
A hacker, a hammer and two memory modules
1
Attacking a co-hosted VM A hacker, a hammer and two memory modules - - PowerPoint PPT Presentation
Attacking a co-hosted VM A hacker, a hammer and two memory modules 1 Who we are (1) Mehdi Talbi Security researcher at Stormshield haka-security project. Past life: academia (phd, postdoc, ) Main research topics: advanced
A hacker, a hammer and two memory modules
1
techniques, abusing virtual machines (phrack paper), low-level stuff, …
@abu_y0ussef mtalbi.github.io
2
3
exploitation, software engineering
Goal: authenticate on the victim VM - given that:
4
5
5
6
7
periodically)
(discharging)
(recharging)
recharging) -> Disturbance errors -> high discharge rate on adjacent row’s cells -> bit-flips in adjacent memory rows (if they are not refreshed before they lose their charge)
8
9
1 code1a: 2 mov (X), %eax /* read from address X */ 3 mov (Y), %ebx /* read from address Y */ 4 clflush (X) /* flush cache for address X */ 5 clflush (Y) /* flush cache for address Y */ 6 jmp code1a
10
June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them 10
June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them March 2015 - Mark Seaborn and Thomas Dullien Exploiting the DRAM rowhammer bug to gain kernel privileges 10
June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them March 2015 - Mark Seaborn and Thomas Dullien Exploiting the DRAM rowhammer bug to gain kernel privileges May 2016 - Erik Bosman et al. Dedup Est Machina 10
June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them March 2015 - Mark Seaborn and Thomas Dullien Exploiting the DRAM rowhammer bug to gain kernel privileges August 2016 - Kaveh Razavi et al. Flip Feng Shui May 2016 - Erik Bosman et al. Dedup Est Machina 10
June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them March 2015 - Mark Seaborn and Thomas Dullien Exploiting the DRAM rowhammer bug to gain kernel privileges August 2016 - Kaveh Razavi et al. Flip Feng Shui May 2016 - Erik Bosman et al. Dedup Est Machina October 2016 - Victor Van Der Venn et al. Drammer 10
both sides (ranks)
11
Total Size = 2 modules * 2 ranks * 2^3 chips * 2^3 banks * 2^15 rows * 2^10 columns * 1 byte = 8 GB
12
13
Sandy Bridge microarchitecture:
((addr >> 14) & 7) ^ ((addr >> 18) & 7)
13
14
such that:
14
such that:
14
such that:
14
allocate huge pages of 2 MB size.
page size row size = 2 × 220 218 = 8 rows
15
16
large buffer (available physical RAM) aligned on 2 MB boundary.
16
large buffer (available physical RAM) aligned on 2 MB boundary.
guest will try to back the buffer by huge pages.
16
large buffer (available physical RAM) aligned on 2 MB boundary.
guest will try to back the buffer by huge pages.
khugepaged running in the host.
16
17
17
about banks?
17
about banks?
bi1bi2bi3
bj1bj2bj3
bi3 = bj3 bi1bi2 6= bj1bj2
17
18
1 static void hammer_byte(uintptr_t aggressors[]) 2 { 3 volatile uint64_t *a = (volatile uint64_t *)aggressors[0]; 4 volatile uint64_t *b = (volatile uint64_t *)aggressors[1]; 5 volatile uint64_t *c = (volatile uint64_t *)aggressors[2]; 6 volatile uint64_t *d = (volatile uint64_t *)aggressors[3]; 7 8 int nb_reads = READ_REPZ; 9 10 while (nb_reads-- > 0) { 11 *a; 12 *b; 13 *c; 14 *d; 15 asm volatile ( 16 "clflush (%0)\n\t" 17 "clflush (%1)\n\t" 18 "clflush (%2)\n\t" 19 "clflush (%3)\n\t" 20 : 21 : "r" (a), "r" (b), "r" (c), "r" (d) 22 : "memory" 23 ); 24 } 25 }
19
Before Merging
20
After Merging
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content.
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
by trying to authenticate on the victim VM)
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
by trying to authenticate on the victim VM)
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
by trying to authenticate on the victim VM)
21
Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content. Hint: Row-hammering is reproducible
by trying to authenticate on the victim VM)
21
especially in virtual machine environments.
with identical content (100 pages every 20 ms.)
flag MADV_MERGEABLE set.
are unmerged during CoW.
22
23
merging.
23
merging.
after a complete scan.
23
merging.
after a complete scan.
tree THEN from the unstable tree.
23
by the physical page controlled by the attacker ?
24
by the physical page controlled by the attacker ?
for merging. The merge is performed from the unstable tree
24
by the physical page controlled by the attacker ?
for merging. The merge is performed from the unstable tree
attacker VM so that the future merges are performed from the stable tree.
24
25
25
content.
25
content.
25
content.
—> Solution: Add some entropy to memory pages (we fill the top of every 4 KB page with 8 random bytes)
25
26
27
28
in the program that can change the outcome of P ?
28
in the program that can change the outcome of P ?
the target function.
28
29
Offset Bit Direction Original Instruction Patched Instruction 0x34c6 1 0 —> 1 test eax, eax xchg eax, eax 0x34c8 0 —> 1 je 0x3538 jne 0x3538 0x34c8 2 1 —> 0 jo 0x3538 0x34c8 3 0 —> 1 jl 0x3538 0x34c8 6 1 —> 0 xor al, 0x6e 0x3520 3 1 —> 0 mov eax, ebx mov eax, edx 0x3520 4 1 —> 0 mov eax, ecx 0x3520 5 0 —> 1 mov eax, edi 0x36c0 1 0 —> 1 mov ebx, eax mov eax, ebx 0x36c1 1 —> 0 mov edx, eax 0x36c1 1 1 —> 0 mov ecx, eax 0x36c1 2 0 —> 1 mov edi, eax 0x36c1 3 0 —> 1 mov ebx, ecx 0x36c1 4 0 —> 1 mov ebx, edx 0x6211 3 0 —> 1 xor eax, eax xor eax, edx 0x6211 4 0 —> 1 xor eax, ecx 0x6211 5 0 —> 1 xor eax, esp 30
31
31
31
bit-flips in the victim row.
31
bit-flips in the victim row.
matches one of the target offsets.
31
bit-flips in the victim row.
matches one of the target offsets.
31
bit-flips in the victim row.
matches one of the target offsets.
31
bit-flips in the victim row.
matches one of the target offsets.
question.
31
bit-flips in the victim row.
matches one of the target offsets.
question.
31
32
32
33
CoW —> page un-merging. —> Noticeable difference in time between writing to a duplicated page and writing to an unshared page.
33
CoW —> page un-merging. —> Noticeable difference in time between writing to a duplicated page and writing to an unshared page.
33
34
Barresi et al.:
34
Barresi et al.:
(4KB each)
34
Barresi et al.:
(4KB each)
34
Barresi et al.:
(4KB each)
in its corresponding index in the second half of the buffer
34
Barresi et al.:
(4KB each)
in its corresponding index in the second half of the buffer
half and measure writing operation time
34
35
corrupting the state of pam_unix.so module.
35
corrupting the state of pam_unix.so module.
https://github.com/mtalbi/pwnpam
35
corrupting the state of pam_unix.so module.
https://github.com/mtalbi/pwnpam
35
corrupting the state of pam_unix.so module.
https://github.com/mtalbi/pwnpam
35
corrupting the state of pam_unix.so module.
echo 0 > /sys/kernel/mm/ksm/run
https://github.com/mtalbi/pwnpam
35
researchers.
36
Cristiano Giuffrida and Herbert Bos. Flip Feng Shui: Hammering a Needle in the Software Stack. In USENIX
Thomas R. Gross. CAIN: Silently Breaking ASLR in the
DRAM rowhammer bug to gain kernel privileges. BH US 2015.
37
38