Attacking a co-hosted VM A hacker, a hammer and two memory modules - - PowerPoint PPT Presentation

attacking a co hosted vm
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Attacking a co-hosted VM

A hacker, a hammer and two memory modules

1

slide-2
SLIDE 2

Who we are (1)

Mehdi Talbi

  • Security researcher at Stormshield
  • haka-security project.
  • Past life: academia (phd, postdoc, …)
  • Main research topics: advanced exploitation

techniques, abusing virtual machines (phrack paper), low-level stuff, …

@abu_y0ussef mtalbi.github.io

2

slide-3
SLIDE 3

Who we are (2)

Paul Fariello

3

  • Security engineer at Stormshield
  • haka-security project.
  • Past life: Software developer at Stormshield
  • Main interest: low-level stuff, vulnerability

exploitation, software engineering

slide-4
SLIDE 4

The attack

Goal: authenticate on the victim VM - given that:

  • Credentials are not known to the attacker
  • Victim VM is not vulnerable to any software bug

4

slide-5
SLIDE 5

Teaser

5

slide-6
SLIDE 6

Teaser

5

slide-7
SLIDE 7

Agenda

  • Row-hammering (from a VM)
  • Abusing memory de-duplication
  • Pwning the libpam
  • Conclusion

6

slide-8
SLIDE 8

Row-hammering

7

slide-9
SLIDE 9

The Bug

  • DRAM Chip: rows of cells (refreshed

periodically)

  • Read/Write operation
  • Data is transferred to the row-buffer

(discharging)

  • Data is copied back to the original row

(recharging)

  • Frequent row activation (discharging +

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

slide-10
SLIDE 10

9

The Code

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

slide-11
SLIDE 11

Past attacks - Timeline

10

slide-12
SLIDE 12

Past attacks - Timeline

June 2014 - Yoongu Kim et al. Flipping bits in memory without accessing them 10

slide-13
SLIDE 13

Past attacks - Timeline

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

slide-14
SLIDE 14

Past attacks - Timeline

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

slide-15
SLIDE 15

Past attacks - Timeline

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

slide-16
SLIDE 16

Past attacks - Timeline

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

slide-17
SLIDE 17

Channels, ranks, banks

  • channel: multiple memory modules
  • memory module: multiple memory chips may be available on

both sides (ranks)

  • memory chips made of banks
  • bank: matrix (column X rows) of memory cells.

11

slide-18
SLIDE 18

DRAM Config.

  • 8 GB of RAM
  • 2 channels
  • 1 memory module / channel
  • 2 ranks / memory module
  • 8 memory chips / rank
  • 8 bank / chip
  • 2^15 rows x 2^10 columns x 8 bits / bank

Total Size = 2 modules * 2 ranks * 2^3 chips * 2^3 banks * 2^15 rows * 2^10 columns * 1 byte = 8 GB

12

slide-19
SLIDE 19

Memory Geometry

  • How physical addresses map to channels, ranks, banks ?

13

slide-20
SLIDE 20

Memory Geometry

  • How physical addresses map to channels, ranks, banks ?
  • Mark Seaborn has determined the address mapping for

Sandy Bridge microarchitecture:

  • Bits 0-5: lower 6 bits of byte index within a row
  • Bit 6: channel selection
  • Bits 7-13: higher 7 bits of byte index within a row
  • Bits 14-16: bank selection obtained by:

((addr >> 14) & 7) ^ ((addr >> 18) & 7)

  • Bit 17: rank selection
  • Bit 18-32: row selection

13

slide-21
SLIDE 21

Row-hammering from a VM

14

slide-22
SLIDE 22

Row-hammering from a VM

  • How to pick a couple of addresses (a_1, a_2) to hammer

such that:

  • a_1 is in row r,
  • a_2 is in row r+2,
  • a_1 and a_2 are in the same bank b ?

14

slide-23
SLIDE 23

Row-hammering from a VM

  • How to pick a couple of addresses (a_1, a_2) to hammer

such that:

  • a_1 is in row r,
  • a_2 is in row r+2,
  • a_1 and a_2 are in the same bank b ?
  • We don’t have access to host’s pagemap file :-(

14

slide-24
SLIDE 24

Row-hammering from a VM

  • How to pick a couple of addresses (a_1, a_2) to hammer

such that:

  • a_1 is in row r,
  • a_2 is in row r+2,
  • a_1 and a_2 are in the same bank b ?
  • We don’t have access to host’s pagemap file :-(
  • Option: Transparent Huge Pages

14

slide-25
SLIDE 25

Transparent Huge Pages

  • khugepaged is a kernel thread that attempts to

allocate huge pages of 2 MB size.

  • A huge page covers multiple rows:
  • We can navigate through rows !!

page size row size = 2 × 220 218 = 8 rows

15

slide-26
SLIDE 26

Transparent Huge Pages

16

slide-27
SLIDE 27

Transparent Huge Pages

  • We allocate in the guest a

large buffer (available physical RAM) aligned on 2 MB boundary.

16

slide-28
SLIDE 28

Transparent Huge Pages

  • We allocate in the guest a

large buffer (available physical RAM) aligned on 2 MB boundary.

  • khugepaged running in the

guest will try to back the buffer by huge pages.

16

slide-29
SLIDE 29

Transparent Huge Pages

  • We allocate in the guest a

large buffer (available physical RAM) aligned on 2 MB boundary.

  • khugepaged running in the

guest will try to back the buffer by huge pages.

  • The same goes for

khugepaged running in the host.

16

slide-30
SLIDE 30

Row Selection

17

slide-31
SLIDE 31

Row Selection

  • Hammer each pair of rows i, i+2 (double-sided row-hammer).

17

slide-32
SLIDE 32

Row Selection

  • Hammer each pair of rows i, i+2 (double-sided row-hammer).
  • We can select pair of addresses from (i, i+2) that belong to the same rank, channel. How

about banks?

17

slide-33
SLIDE 33

Row Selection

  • Hammer each pair of rows i, i+2 (double-sided row-hammer).
  • We can select pair of addresses from (i, i+2) that belong to the same rank, channel. How

about banks?

  • Let and bits identifying a bank in row i and j, respectively.
  • We hammer each address from row i with the address from j (j = i + 2) satisfying this condition:

bi1bi2bi3

bj1bj2bj3

bi3 = bj3 bi1bi2 6= bj1bj2

17

slide-34
SLIDE 34

18

Quad-Hammering

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 }

slide-35
SLIDE 35

Abusing memory de-duplication

19

slide-36
SLIDE 36

Memory De-duplication

Before Merging

20

After Merging

slide-37
SLIDE 37

Attack Steps

21

slide-38
SLIDE 38

Attack Steps

Goal: Modify a bit in a file (e.g. pam_unix.so) hosted in the victim VM assuming we know its content.

21

slide-39
SLIDE 39

Attack Steps

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

slide-40
SLIDE 40

Attack Steps

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

  • 1. Map the available physical memory

21

slide-41
SLIDE 41

Attack Steps

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

  • 1. Map the available physical memory
  • 2. Hammer the memory from attacker VM

21

slide-42
SLIDE 42

Attack Steps

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

  • 1. Map the available physical memory
  • 2. Hammer the memory from attacker VM
  • 3. Load the target file in the victim VM (e.g.

by trying to authenticate on the victim VM)

21

slide-43
SLIDE 43

Attack Steps

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

  • 1. Map the available physical memory
  • 2. Hammer the memory from attacker VM
  • 3. Load the target file in the victim VM (e.g.

by trying to authenticate on the victim VM)

  • 4. Wait for memory deduplication.

21

slide-44
SLIDE 44

Attack Steps

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

  • 1. Map the available physical memory
  • 2. Hammer the memory from attacker VM
  • 3. Load the target file in the victim VM (e.g.

by trying to authenticate on the victim VM)

  • 4. Wait for memory deduplication.
  • 5. Hammer again

21

slide-45
SLIDE 45

Attack Steps

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

  • 1. Map the available physical memory
  • 2. Hammer the memory from attacker VM
  • 3. Load the target file in the victim VM (e.g.

by trying to authenticate on the victim VM)

  • 4. Wait for memory deduplication.
  • 5. Hammer again
  • 6. Enjoy !!!

21

slide-46
SLIDE 46

Kernel Same-Page Merging

  • KSM reduces considerably the memory footprint,

especially in virtual machine environments.

  • KSM periodically scans the memory looking for pages

with identical content (100 pages every 20 ms.)

  • KSM merges identical anonymous pages having the

flag MADV_MERGEABLE set.

  • Writing to a merged page triggers a page fault. Pages

are unmerged during CoW.

22

slide-47
SLIDE 47

KSM Internals

23

slide-48
SLIDE 48
  • KSM maintains two red-black trees:
  • The stable tree: keeps track of shared pages.
  • The unstable tree: stores pages that are candidate for

merging.

KSM Internals

23

slide-49
SLIDE 49
  • KSM maintains two red-black trees:
  • The stable tree: keeps track of shared pages.
  • The unstable tree: stores pages that are candidate for

merging.

  • The unstable tree is trashed and reconstructed

after a complete scan.

KSM Internals

23

slide-50
SLIDE 50
  • KSM maintains two red-black trees:
  • The stable tree: keeps track of shared pages.
  • The unstable tree: stores pages that are candidate for

merging.

  • The unstable tree is trashed and reconstructed

after a complete scan.

  • FIRST, KSM tries to merge pages from the stable

tree THEN from the unstable tree.

KSM Internals

23

slide-51
SLIDE 51

« Winning » the Merge

  • How to guarantee that KSM will back merged pages

by the physical page controlled by the attacker ?

24

slide-52
SLIDE 52

« Winning » the Merge

  • How to guarantee that KSM will back merged pages

by the physical page controlled by the attacker ?

  • Option 1: Starting the attacker VM before the victim
  • VM. KSM will keep the page that has registered first

for merging. The merge is performed from the unstable tree

24

slide-53
SLIDE 53

« Winning » the Merge

  • How to guarantee that KSM will back merged pages

by the physical page controlled by the attacker ?

  • Option 1: Starting the attacker VM before the victim
  • VM. KSM will keep the page that has registered first

for merging. The merge is performed from the unstable tree

  • Option 2: Load two copies of the target file in the

attacker VM so that the future merges are performed from the stable tree.

24

slide-54
SLIDE 54

KSM Vs THP

25

slide-55
SLIDE 55

KSM Vs THP

  • THP merges 4 KB pages to form huge pages.

25

slide-56
SLIDE 56

KSM Vs THP

  • THP merges 4 KB pages to form huge pages.
  • KSM merges 4 KB pages sharing the same

content.

25

slide-57
SLIDE 57

KSM Vs THP

  • THP merges 4 KB pages to form huge pages.
  • KSM merges 4 KB pages sharing the same

content.

  • KSM can break our huge pages :-(

25

slide-58
SLIDE 58

KSM Vs THP

  • THP merges 4 KB pages to form huge pages.
  • KSM merges 4 KB pages sharing the same

content.

  • KSM can break our huge pages :-(

—> Solution: Add some entropy to memory pages (we fill the top of every 4 KB page with 8 random bytes)

25

slide-59
SLIDE 59

Pwning the Libpam

26

slide-60
SLIDE 60

RE the Libpam

  • Libpam: Attractive target —> provides authentication mechanisms
  • n widely deployed *.nix systems.
  • pam_sm_authenticate [0x3440]: performs the task of authenticating a user.
  • _unix_blankpassword [0x6130]: checks if the user does not have a blank password.

27

slide-61
SLIDE 61

Flippable Bits

28

slide-62
SLIDE 62

Flippable Bits

  • Given a program P, how to find all « flippable » bits

in the program that can change the outcome of P ?

28

slide-63
SLIDE 63

Flippable Bits

  • Given a program P, how to find all « flippable » bits

in the program that can change the outcome of P ?

  • Use of timeless-debugging capabilities:
  • 1. Flip a bit of a some target function.
  • 2. Run the target function.
  • 3. Check whether the flipped bit impacts the expected result of

the target function.

  • 4. Restore session and proceed with next bit.

28

slide-64
SLIDE 64

Flippable Bits

29

slide-65
SLIDE 65

Flippable Bits

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

slide-66
SLIDE 66

The Attack

31

slide-67
SLIDE 67

The Attack

  • 1. Allocate the available physical memory.

31

slide-68
SLIDE 68

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.

31

slide-69
SLIDE 69

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

31

slide-70
SLIDE 70

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

  • 4. Load pam_unix.so module in the victim page in the attacker VM if the bit-flip

matches one of the target offsets.

31

slide-71
SLIDE 71

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

  • 4. Load pam_unix.so module in the victim page in the attacker VM if the bit-flip

matches one of the target offsets.

  • 5. Load pam_unix.so module in the victim VM by trying to log on.

31

slide-72
SLIDE 72

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

  • 4. Load pam_unix.so module in the victim page in the attacker VM if the bit-flip

matches one of the target offsets.

  • 5. Load pam_unix.so module in the victim VM by trying to log on.
  • 6. Wait for KSM to merge the pages.

31

slide-73
SLIDE 73

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

  • 4. Load pam_unix.so module in the victim page in the attacker VM if the bit-flip

matches one of the target offsets.

  • 5. Load pam_unix.so module in the victim VM by trying to log on.
  • 6. Wait for KSM to merge the pages.
  • 7. Hammer again the aggressor addresses that have produced the bit-flip in

question.

31

slide-74
SLIDE 74

The Attack

  • 1. Allocate the available physical memory.
  • 2. Add some entropy to memory pages to prevent KSM from breaking THP pages.
  • 3. Hammer each pair of aggressor rows in every huge page and check if we have

bit-flips in the victim row.

  • 4. Load pam_unix.so module in the victim page in the attacker VM if the bit-flip

matches one of the target offsets.

  • 5. Load pam_unix.so module in the victim VM by trying to log on.
  • 6. Wait for KSM to merge the pages.
  • 7. Hammer again the aggressor addresses that have produced the bit-flip in

question.

  • 8. Enjoy !!

31

slide-75
SLIDE 75

Demo

32

slide-76
SLIDE 76

Demo

32

slide-77
SLIDE 77

Going Further

33

slide-78
SLIDE 78

Going Further

  • Modification to a merged page —> page fault —>

CoW —> page un-merging. —> Noticeable difference in time between writing to a duplicated page and writing to an unshared page.

33

slide-79
SLIDE 79

Going Further

  • Modification to a merged page —> page fault —>

CoW —> page un-merging. —> Noticeable difference in time between writing to a duplicated page and writing to an unshared page.

  • Exploiting a side-channel timing attack in KSM:
  • Detect when two pages are merged.
  • Detect the version of the Libpam in the victim VM.

33

slide-80
SLIDE 80

CAIN Attack

34

slide-81
SLIDE 81

CAIN Attack

  • Cain attack by Antonio

Barresi et al.:

34

slide-82
SLIDE 82

CAIN Attack

  • Cain attack by Antonio

Barresi et al.:

  • 1. Allocate a large buffer on N elements

(4KB each)

34

slide-83
SLIDE 83

CAIN Attack

  • Cain attack by Antonio

Barresi et al.:

  • 1. Allocate a large buffer on N elements

(4KB each)

  • 2. Fill buffer with random

34

slide-84
SLIDE 84

CAIN Attack

  • Cain attack by Antonio

Barresi et al.:

  • 1. Allocate a large buffer on N elements

(4KB each)

  • 2. Fill buffer with random
  • 3. Copy each even element of the first half

in its corresponding index in the second half of the buffer

34

slide-85
SLIDE 85

CAIN Attack

  • Cain attack by Antonio

Barresi et al.:

  • 1. Allocate a large buffer on N elements

(4KB each)

  • 2. Fill buffer with random
  • 3. Copy each even element of the first half

in its corresponding index in the second half of the buffer

  • 4. Modify a byte in each element of the first

half and measure writing operation time

34

slide-86
SLIDE 86

Conclusion

35

slide-87
SLIDE 87

Conclusion

  • Successful row-hammer attack on a co-hosted VM:

corrupting the state of pam_unix.so module.

35

slide-88
SLIDE 88

Conclusion

  • Successful row-hammer attack on a co-hosted VM:

corrupting the state of pam_unix.so module.

  • Weaponizing row-hammer attacks.

https://github.com/mtalbi/pwnpam

35

slide-89
SLIDE 89

Conclusion

  • Successful row-hammer attack on a co-hosted VM:

corrupting the state of pam_unix.so module.

  • Weaponizing row-hammer attacks.

https://github.com/mtalbi/pwnpam

35

slide-90
SLIDE 90

Conclusion

  • Successful row-hammer attack on a co-hosted VM:

corrupting the state of pam_unix.so module.

  • Weaponizing row-hammer attacks.
  • Row-hammer attacks are powerful and effective.

https://github.com/mtalbi/pwnpam

35

slide-91
SLIDE 91

Conclusion

  • Successful row-hammer attack on a co-hosted VM:

corrupting the state of pam_unix.so module.

  • Weaponizing row-hammer attacks.
  • Row-hammer attacks are powerful and effective.
  • Mitigation:
  • ECC RAM
  • Disabling KSM

echo 0 > /sys/kernel/mm/ksm/run

https://github.com/mtalbi/pwnpam

35

slide-92
SLIDE 92

Greetz

  • Pierre Sylvain Desse
  • Stormshield
  • Kudos to Mark Seaborn, @halvarflake and @vu5ec

researchers.

36

slide-93
SLIDE 93

References

  • [1] Kaveh Razavi, Ben Gras, Erik Bosman, Bart Preneel,

Cristiano Giuffrida and Herbert Bos. Flip Feng Shui: Hammering a Needle in the Software Stack. In USENIX

  • Security. 2016.
  • [2] Antonio Barresi, Kaveh Razavi, Mathias Payer and

Thomas R. Gross. CAIN: Silently Breaking ASLR in the

  • Cloud. In USENIX Workshop on Offensive Technologies.
  • [3] Mark Seaborn and Thomas Dullien. Exploiting the

DRAM rowhammer bug to gain kernel privileges. BH US 2015.

37

slide-94
SLIDE 94

THANK YOU

38